简体   繁体   中英

cmd.exe, append text on new line

I am trying to script appending a line to a file on multiple windows machines. the command echo text >> file.txt does not put the word text on a newline, but on the last line. I cannot seem to get \n or \r to work with echo on windows. help!

I assume the last line in file.txt is not terminated by a line-break, so you can explicitly append such:

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM