Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows: in batch file, write multiple lines to text file?

People also ask

How do I echo multiple lines in a text file?

To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.

How do you write multiple lines in command prompt?

The Windows command prompt (cmd.exe) allows the ^ (Shift + 6) character to be used to indicate line continuation. It can be used both from the normal command prompt (which will actually prompt the user for more input if used) and within a batch file.

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do you write a batch file to text?

Click File and then Save, and then navigate to where you want to save the file. For the file name, type test. bat and if your version of Windows has a Save as type option, choose All files, otherwise it saves as a text file. Once you have completed these steps, click the Save button and exit notepad.


Use output redirection > and >>

echo one>%file%
echo two>>%file%
echo three>>%file%

Or in a more readable way: (In cmd.exe, using "echo one >%file%" would include the whitespace before >.)

>%file%  echo one
>>%file% echo two
>>%file% echo three

You could also use:

(
    echo one
    echo two
    echo three
) >%file%

echo Line 1^

Line 2^

Line 3 >textfile.txt

Note the double new-lines to force the output:

Line1
Line2
Line3

Also:

(echo Line 1^

Line 2^

Line 3)>textfile.txt