使用 Windows 批处理命令附加到文件时,如何在文件中的下一个单词之后立即附加?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2539337/
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
When appending to a file using Windows batch commands, how to append immediately after the next word in the file?
提问by Arunachalam
When appending to a file using Windows batch commands, how to append immediately after the next word in the file?
使用 Windows 批处理命令附加到文件时,如何在文件中的下一个单词之后立即附加?
For example, these commands
例如,这些命令
echo here is the date of > c:\arun.txt
date /t >> c:\arun.txt
write the following text to the arun.txt file:
将以下文本写入 arun.txt 文件:
here is the date of
29-03-2010
这是
29-03-2010的日期
But I want the output to be like this:
但我希望输出是这样的:
here is the date of 29-03-2010
这是 29-03-2010 的日期
How to avoid carrage return while appending?
附加时如何避免回车?
回答by Helen
The echo
output always includes a trailing new line. To output text without a trailing new line, you can use the set /p
trick described hereand here:
的echo
输出总是包括后新行。要输出没有尾随换行符的文本,您可以使用此处和此处set /p
描述的技巧:
< nul (set /p s=Today is ) > c:\arun.txt
date /t >> c:\arun.txt
But in this specific case, you can simply use the %date%
variable instead of date /t
, as %date%
uses the same format:
但在这种特定情况下,您可以简单地使用%date%
变量而不是date /t
,因为%date%
使用相同的格式:
echo Today is %date% > c:\arun.txt
回答by ghostdog74
you can store to a variable and append
您可以存储到变量并附加
C:\test>set s=here is today's date
C:\test>for /F "tokens=*" %i in ('date /t') do set d=%i
C:\test>set d=Tue 03/30/2010
C:\test>echo %d%%s%
Tue 03/30/2010 here is today's date