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

前端 未结 3 450
南旧
南旧 2020-12-13 08:24

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. Ther

相关标签:
3条回答
  • 2020-12-13 09:01

    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
    
    0 讨论(0)
  • 2020-12-13 09:04

    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.

    0 讨论(0)
  • 2020-12-13 09:05
    (
    echo Here is my first line
    echo Here is my second line
    echo Here is my third line
    )>"myNewTextFile.txt"
    pause
    
    0 讨论(0)
提交回复
热议问题