windows 如何从批处理脚本将“2”(无引号)回显到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7225630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to echo "2" (no quotes) to a file, from a batch script?
提问by user541686
How do I echo the number 2into a file, from a batch script?
如何从批处理脚本将数字2回显到文件中?
This doesn't work:
这不起作用:
Echo 2>> file.txt
because 2>>
is a special command. :(
因为2>>
是一个特殊的命令。:(
采纳答案by Dominic K
Use (ECHO 2)>>file.txt
. This will output 2
without any spaces.
使用(ECHO 2)>>file.txt
. 这将输出2
没有任何空格。
回答by Raymond Chen
Little-known feature: The redirection operator can go anywhere on the line.
鲜为人知的特性:重定向操作符可以到线路上的任何地方。
>>file.txt echo 2
回答by lavinio
echo ^2>>file.txt
appears to work for me.
echo ^2>>file.txt
似乎对我有用。
回答by Moe Sisko
Use the ^
escape :
使用^
转义:
Echo ^2>> file.txt
回答by Kornel Kisielewicz
echo.2>>text.txt
Yes, it's weird.
是的,这很奇怪。
回答by walid2mi
another method
另一种方法
echo>>file.txt 2
回答by jeb
Or you can use the delayed expansion
或者你可以使用延迟扩展
setlocal EnableDelayedExpansion
set "var=2"
echo(!var!>> file.txt
回答by minhng99
If you need an accurate write, use this command
如果您需要准确的写入,请使用此命令
(ECHO |set /p=2)>>file.txt
the command by DMan will produce a 0x0D0A line break behind the "2"
DMan 的命令将在“2”后面产生一个 0x0D0A 换行符
回答by aschipfl
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
).
这是有效的,因为set /A
在命令提示符上下文中执行时返回(最后一个)结果(没有换行符)(因此直接在 中运行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.
虽然你不能在批处理文件中使用它,但你需要额外的cmd /C
来强制正确的执行上下文。
Needless to say, this can of course only output numbers, or precisely said, signed 32-bit integers.
不用说,这当然只能输出数字,或者准确地说,有符号的 32 位整数。