Batch Delete a line in a text file?

20,491

You can use more +1 to skip the first line of the file. Then you can pipe it into a temporary one (you cannot edit text files in place):

for %x in (*.txt) do (more +1 "%x">tmp & move /y tmp "%x")

After that you can use a similar technique to re-add the first line:

for %x in (*.txt) do ((echo X,Y,Z& type "%x")>tmp & move /y tmp "%x")

If you use those in a batch file, remember to double the % signs:

@echo off
for %%x in (*.txt) do (
    more +1 "%%x" >tmp
    move /y tmp "%%x"
)
rem Run your utility here
for %%x in (*.txt) do (
    echo X,Y,Z>tmp
    type "%%x" >>tmp
    move /y tmp "%%x"
)

Ok, apparently more doesn't work with too large files, which surprises me. As an alternative, which should work if your file does not contain blank lines (though it looks like CSV from what I gathered):

for %%x in (*.txt) do (
    for /f "skip=1 delims=" %%l in ("%%x") do (>>tmp echo.%%l)
    move /y tmp "%%x"
)
Share:
20,491
Admin
Author by

Admin

Updated on March 28, 2020

Comments

  • Admin
    Admin about 4 years

    I'm pulling my hair out finding a simple example of a DOS batch file that will delete the first line of several thousand txt files and save the file with the original file name. Following a batch process performed by another program, I then have to ADD the deleted line ( a text string consisting of "X,Y,Z") at the beginning of each file following the external processing.

  • Rich
    Rich about 13 years
    for can iterate over lists of files just fine without needing to parse dir's output (which can also fail miserably with Unicode file names in certain circumstances). Furthermore you're missing the removal of the first line.
  • Admin
    Admin about 13 years
    Nope! Spoke to soon! @echo off for %%x in (*.txt) do ( more +1 "%%x" >tmp move /y tmp "%%x" )
  • Admin
    Admin about 13 years
    @echo off for %%x in (*.txt) do ( more +1 "%%x" >tmp move /y tmp "%%x" ) locks up when I move this to production files. Each file is from 5-20 MB in size. The tmp file grows to 2241 kb and thats where it stops working.
  • Rich
    Rich about 13 years
    @Tom: I added another approach, untested yet, but I currently lack the time to thoroughly test. This approach should be much slower, though, sadly.
  • Christoph_J
    Christoph_J over 12 years
    @Joey: I tried your script, but I was running in the same problem as @Tom, namely that it just didn't work for larger files. Your second approach (the one without more), however, doesn't do anything when I try to run it. Do you have any idea why? Basically, the only thing I changed was to replace .txt by .csv in the first line because I want to delete the first line of every csv-file. However, nothing happens (if I look at the save time of the files, this doesn't change, so I guess they are not opened). Would be great if you might have an idea.