Package Deleting a line in a text file?

I pull my hair out, find a simple example of a DOS batch file that will remove the first line of several thousand txt files and save the file with the original file name. After a batch process executed by another program, I have to add the ADD deleted line (a text line consisting of "X, Y, Z") at the beginning of each file following the external processing.

+4
source share
2 answers

You can use more +1 to skip the first line of the file. Then you can connect it to 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 method 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 files in a batch file, be sure 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" ) 

Well, apparently, more does not work with files that are too large, which surprises me. Alternatively, that should work if your file does not contain empty lines (although this is similar to the CSV from what I put together):

 for %%x in (*.txt) do ( for /f "skip=1 delims=" %%l in ("%%x") do (>>tmp echo.%%l) move /y tmp "%%x" ) 
+4
source

Here is my version.

 @echo off for /f "usebackq delims=" %%f in (`dir /b *.txt`) do ( echo X, Y, Z>"tmp.txt" type "%%f" >> tmp.txt move tmp.txt "%%f" ) 
0
source

Source: https://habr.com/ru/post/1345444/


All Articles