Echo string to .txt file with multiple lines - with Windows Batch file
WindowsShellBatch FileWindows Problem Overview
I am attempting to create a Windows Batch File that creates a .txt with mulitple lines. I've tried several solutions to insert a line break in the string but no avail. There are other similar questions/answers but none of them address putting the entire string into a text file.
My batch file currently reads:
echo Here is my first line
Here is my second line > myNewTextFile.txt
pause
my goal is to have the text file read:
Here is my first line
Here is my second line
Obviously, this does not work currently, but wondering if anyone knows how to make this happen in a simple fashion?
Windows Solutions
Solution 1 - Windows
(
echo Here is my first line
echo Here is my second line
echo Here is my third line
)>"myNewTextFile.txt"
pause
Solution 2 - Windows
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.txt
echo Here is my second line >> myNewTextFile.txt
echo Here is my third line >> myNewTextFile.txt
pause
Solution 3 - Windows
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.txt
call :data2 >file2.txt
exit /b
:data1
echo Next line is empty
echo
echo /? this line starts with /?
echo Last line
exit /b
:data2
echo:Next line is empty
echo:
echo:/? this line starts with /?
echo:Last line
exit /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.
Solution 4 - Windows
STEP 1: Enter Line 1 followed by the ^ character.
echo Here is my first line^
STEP 2: Hit RETURN key to get a prompt for more text
echo Here is my first line^
More?
STEP 3: Hit RETURN key once more to get a second prompt for more text
echo Here is my first line^
More?
More?
STEP 4: Continue line 2 from the second prompt
echo Here is my first line^
More?
More? Here is my second line
STEP 5: Hit the RETURN key to get 2 statements displayed on two separate lines
Results:
echo Here is my first line^
More?
More? Here is my second line
Here is my first line
Here is my second line
NOTE
However, if you wish to save this to file, you could add a final STEP.
STEP 6: with the help of the > character, you can append the filename so you save your output to file instead.
echo Here is my first line^
More?
More? Here is my second line >"myNewTextFile.txt"
Solution 5 - Windows
Use this:
echo Here is my first line >> myNewTextFile.txt
echo Here is my second line >> myNewTextFile.txt