Echo string to .txt file with multiple lines - with Windows Batch file Echo string to .txt file with multiple lines - with Windows Batch file windows windows

Echo string to .txt file with multiple lines - with Windows Batch file


(echo Here is my first lineecho Here is my second lineecho Here is my third line)>"myNewTextFile.txt"pause


Just repeat the echo and >> for lines after the first. >> means that it should append to a file instead of creating a new file (or overwriting an existing file):

echo Here is my first line > myNewTextFile.txtecho Here is my second line >> myNewTextFile.txtecho Here is my third line >> myNewTextFile.txtpause


Searching for something else, I stumbled on this meanwhile old question, and I have an additional little trick that is worth mentioning, I think.

All solutions have a problem with empty lines and when a line starts with an option for the echo command itself. Compare the output files in these examples:

call :data1 >file1.txtcall :data2 >file2.txtexit /b:data1echo Next line is emptyechoecho /? this line starts with /?echo Last lineexit /b:data2echo:Next line is emptyecho:echo:/? this line starts with /?echo:Last lineexit /b

Now, file1.txt contains:

Next line is empty ECHO is off. Displays messages, or turns command-echoing on or off.  ECHO [ON | OFF]  ECHO [message]Type ECHO without parameters to display the current echo setting. Last line

While file2.txt contains:

Next line is empty/? this line starts with /?Last line

The use of echo: miraculously solves the issues with the output in file1.txt.

Besides the colon, there are other characters that you could 'paste' to echo, among them a dot, a slash, ... Try for yourself.