Batch file append text new line
A simple but useful question!
To append text to a file and add a new line in a batch file, you can use the following syntax:
echo. >> file.txt
echo Your new text here >> file.txt
Here's what's happening:
echo.
is used to add a blank line to the file. The dot (.
) is a special character in batch files that represents a space, and theecho
command will print it followed by a newline character (\n
).>> file.txt
redirects the output of theecho
command to the filefile.txt
, appending to the end of the file instead of overwriting it.
So, when you run these two lines, the file file.txt
will be created or updated with the following contents:
Your new text here
If you want to add multiple lines of text, you can repeat the echo
command with the desired text:
echo. >> file.txt
echo Your new text here >> file.txt
echo Another line of text >> file.txt
This will result in the following contents in the file:
Your new text here
Another line of text
Note that if you want to add a literal newline character (\n
) to the file, you can use the following syntax:
echo Your new text here^& >> file.txt
The ^&
is an escape sequence that tells the batch file to treat the newline character as a literal character instead of a command separator.