17

I'm using Windows 7 and I would like to quickly create a small text file with a few lines of text in the Command prompt.

I can create a single line text file with:

echo hello > myfile.txt

but how can I create a text file with multiple lines using this echo command? I have tried with the following, which doesn't work when I read the file with more:

echo hello\nsecond line > myfile.txt

Any suggestions? Or is there any other standard command that I can use for this instead of echo?

1

4 Answers 4

18

You could use the >> characters to append a second line to the file, e.g.

echo hello > myfile.txt
echo second line >> myfile.txt
22

There are three ways.

  1. Append each line using >>:

    C:\Users\Elias>echo foo > a.txt
    C:\Users\Elias>echo bar >> a.txt
    
  2. Use parentheses to echo multiple lines:

    C:\Users\Elias>(echo foo
    More? echo bar) > a.txt
    
  3. Type caret (^) and hit ENTER twice after each line to continue adding lines:

    C:\Users\Elias>echo foo^
    More?
    More? bar > a.txt
    

All the above produce the same file:

C:\Users\Elias>type a.txt
foo
bar
2

If you REALLY want to type everything in a single line you can just put an & for each new line, like:

echo hello >> myfile.txt & echo second line >> myfile.txt

but efotinis' answer is the easiest one.

-2

You can put a space between each line to write:

echo line1 line2 "line 3" > file.txt
2
  • This displays everything in one line: line1 line2 "line 3" Feb 11, 2019 at 12:17
  • This works in powershell, not command prompt. Mar 17, 2022 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .