Add new line in text file with Windows batch file Add new line in text file with Windows batch file windows windows

Add new line in text file with Windows batch file


I believe you are using the

echo Text >> Example.txt 

function?

If so the answer would be simply adding a "." (Dot) directly after the echo with nothing else there.

Example:

echo Blahecho Blah 2echo. #New line is addedecho Next Blah


DISCLAIMER: The below solution does not preserve trailing tabs.


If you know the exact number of lines in the text file, try the following method:

@ECHO OFFSET origfile=original fileSET tempfile=temporary fileSET insertbefore=4SET totallines=200<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (  SETLOCAL EnableDelayedExpansion  SET /P L=  IF %%i==%insertbefore% ECHO(  ECHO(!L!  ENDLOCAL)) >%tempfile%COPY /Y %tempfile% %origfile% >NULDEL %tempfile%

The loop reads lines from the original file one by one and outputs them. The output is redirected to a temporary file. When a certain line is reached, an empty line is output before it.

After finishing, the original file is deleted and the temporary one gets assigned the original name.


UPDATE

If the number of lines is unknown beforehand, you can use the following method to obtain it:

FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C

(This line simply replaces the SET totallines=200 line in the above script.)

The method has one tiny flaw: if the file ends with an empty line, the result will be the actual number of lines minus one. If you need a workaround (or just want to play safe), you can use the method described in this answer.


You can use:

type text1.txt >> combine.txtecho >> combine.txttype text2.txt >> combine.txt

or something like this:

echo blah >> combine.txtecho blah2 >> combine.txtecho >> combine.txtecho other >> combine.txt