Add new line in text file with Windows batch file

113,496

Solution 1

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 Blah
echo Blah 2
echo. #New line is added
echo Next Blah

Solution 2

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 OFF
SET origfile=original file
SET tempfile=temporary file
SET insertbefore=4
SET 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% >NUL
DEL %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.

Solution 3

You can use:

type text1.txt >> combine.txt
echo >> combine.txt
type text2.txt >> combine.txt

or something like this:

echo blah >> combine.txt
echo blah2 >> combine.txt
echo >> combine.txt
echo other >> combine.txt
Share:
113,496
newbie18
Author by

newbie18

Updated on October 08, 2020

Comments

  • newbie18
    newbie18 over 3 years

    I have a text file which has more than 200 lines in it, and I just want to add a new line before line 4. I'm using Windows XP.

    Example text file before input:

    header 1
    header 2
    header 3
    details 1
    details 2
    

    After output:

    header 1
    header 2
    header 3
    <----- This is new line ---->
    details 1
    details 2
    
  • newbie18
    newbie18 over 12 years
    1) this script i save as run1.bat. i test to run it, got error msg (the syntax of command is incorrect.) the file rename as after.txt
  • newbie18
    newbie18 over 12 years
    thanks your reply james Dyson. i need to edit the existing file instead of new file
  • newbie18
    newbie18 over 12 years
    2) the format is out of order <br/> for example: input header 1<--here got tab delimited format--><--here got tab delimited format--> header 2<--here got tab delimited format--><--here got tab delimited format--> header 3<--here got tab delimited format--><--here got tab delimited format--> details 1 details 2 output: header 1<--tab delimited is missing--><--tab delimited is missing--> header 2<--tab delimited is missing--><--tab delimited is missing--> header 3<--tab delimited is missing--><--tab delimited is missing--> details 1 details 2 </pre>
  • Andriy M
    Andriy M over 12 years
    I tested the script too and didn't get any errors. As for your issue #2... Well, using a batch script to process a file with such tender contents is like mowing a lawn with a combine harvester: it might be possible, but requires very thorough adjustments. I re-tested the script and it indeed removes trailing tab characters. So far I have no idea how to work around that. Someone else might. Or maybe you should consider using other tools. You could also try your luck at superuser.
  • newbie18
    newbie18 over 12 years
    item 1 - still have error this is the script. @ECHO OFF <br> SET origfile="C:\Documents and Settings\user\Desktop\test1\before.txt" <br> SET tempfile="C:\Documents and Settings\user\Desktop\test1\after.txt" <br> SET insertbefore=4 <br> FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C <br> <%origfile% (FOR /L %%i IN (1,1,%totallines%) DO ( <br> SETLOCAL EnableDelayedExpansion <br> SET /P L= <br> IF %%i==%insertbefore% ECHO( <br> ECHO(!L! <br> ENDLOCAL <br> )<br> ) >%tempfile% <br> DEL %origfile% <br> RENAME %tempfile% %origfile% <br> pause <br>
  • Andriy M
    Andriy M over 12 years
    I see now, thanks. Please change the last two lines of the script like in my updated answer. The problem was, the second argument of the RENAME command should be just a name, with no path.
  • Ahmad
    Ahmad over 12 years
    using the >> to direct the output to file will append to the end of the file. If you use a single chevron then the output will overwrite the existing file if it does exist, or create a new file if it does not.
  • mtalexan
    mtalexan over 10 years
    This won't work in Windows because the echo command with nothing following it actually prints the echo state, "ECHO is ON" for example.
  • Saneesh kumar
    Saneesh kumar about 6 years
    Though it is an old post, if someone wants to use this solution in windows, please use echo. >> combine.txt. The additional "dot" after echo should solve the "ECHO is ON" problem.
  • oxygen
    oxygen about 6 years
    @Saneeshkumar you should make that into an answer.
  • General Grievance
    General Grievance over 3 years
    I'm not really sure how this answers the question. It seems the text file already exists and the OP just wanted to insert a new line in the middle somewhere, not the end.