74

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?

4 Answers 4

134
(
echo Here is my first line
echo Here is my second line
echo Here is my third line
)>"myNewTextFile.txt"
pause
Sign up to request clarification or add additional context in comments.

11 Comments

The quotes aren't necessary but are good practice, for when & or space characters are in the filename or full path. You will also find that closing brackets need to be escaped as in ^) when using this solution.
Unfortunately this does not work, when brackets '(' are in the text to be echoed. The solution with > and >> still works.
For the sake of clarity in conversation and eliminating the need to type "(", "[", "{"... The proper terms for these characters are: Parentheses: () Brackets, or Square Brackets:[] Braces, or Curly Braces: {}
@Bob Brackets is a general term for all of those marks (including parentheses, which can certainly be called "round brackets"). Using "brackets" without qualification to mean square brackets [] specifically is only common in the US (and not even universal there), while in the British English it would mean round brackets (). This makes more sense any IMO since outside of computing the most commonly used brackets are round by an enormous margin. In any case, it certainly makes sense to just type out the type (square, round, curly) and type them out explicitly rather than risk confusion.
@michael_s The solution to this is in the comment right above yours: escape any round brackets with a caret ^
|
40

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

4 Comments

Is the pause command at the end required or is it just added because the OP has it in his script?
@AdityaVikasDevarapalli: It's just there because it was in the poster's code. It is not required or necessary.
The accepted answer didn't work for me while this one did.
Note that this will write spaces at the end of lines, because there is a space between the last word and >. Not normally a problem, but a hard one with some machine-readable files. Remove the spaces from the source code to fix.
11

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.

1 Comment

Nice to know that colon works as well, I thought the documented version was dot as in echo. and echo./? whatever
3

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"

Example from CMD

1 Comment

this solution only works in command prompt, not in a batch files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.