[英]cmd.exe, append text on new line
I am trying to script appending a line to a file on multiple windows machines.我正在尝试编写脚本在多台 windows 机器上的文件中附加一行。 the command
echo text >> file.txt
does not put the word text
on a newline, but on the last line.命令
echo text >> file.txt
不会将单词text
放在换行符上,而是放在最后一行。 I cannot seem to get \n or \r to work with echo on windows.我似乎无法让 \n 或 \r 在 windows 上使用 echo。 help!
帮助!
I assume the last line in file.txt
is not terminated by a line-break, so you can explicitly append such:我假设
file.txt
中的最后一行没有被换行符终止,所以你可以明确地 append 这样:
rem // Append line-break plus text:
(echo/&echo text) >> file.txt
If file.txt
may or may not be terminated by a line-break, you could use find
:如果
file.txt
可能会或可能不会被换行符终止,您可以使用find
:
rem // Use `find` to force a final line-break, then append text and write to temporary file:
(find /V "" < file.txt & echo text) > file.txt.tmp
rem // Move temporary file onto original one:
move /Y file.txt.tmp file.txt
Note that find
limits line lengths to 4095 characters or bytes.请注意,
find
将行长度限制为 4095 个字符或字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.