How to echo “2” (no quotes) to a file, from a batch script?

后端 未结 11 1392
孤街浪徒
孤街浪徒 2020-12-02 22:13

How do I echo the number 2 into a file, from a batch script?

This doesn\'t work:

Echo 2>> file.txt

because 2&g

11条回答
  •  醉酒成梦
    2020-12-02 22:25

    To write a number to a file without a trailing line-break you could use the following:

    cmd /C set /A "2">file.txt
    

    This works, because set /A returns the (last) result (without line-break) when executed in command prompt context (hence when directly run in cmd.exe).

    If you are working in command prompt anyway, you could simply use this:

    set /A "2">file.txt
    

    Though you cannot use that in a batch file, you need the extra cmd /C to force the right execution context.

    Needless to say, this can of course only output numbers, or precisely said, signed 32-bit integers.

提交回复
热议问题