1.

Solve : Delete lines in text files?

Answer»

Hi

I have a folder that contains 6000 text files. I need to delete the 3rd last line in each file. I need a DOS program to loop through all files and delete the line. Can you post code PLEASE?

ThanksIs this a homework question?No its not a homework question. Considered implementaions in Java and VB but I assume a more strightforwrd approach can be achieved using DOS. I have no EXPERIENCE in DOS hence the question

ThanksI'm tempted to go off-topic and ask exactly what you're doing with 6000 text files. I don't think I have that MANY files on my WHOLE computer! *does search for "*.*"* Oh, wait, yes I do...

I don't think that can be done in DOS. I'd be hard pressed to find a way to do it in VB as well. I think it can be done in VB, but I'm pretty sure DOS is out of the question.The next code delete the 3rd last line for the file that you write in the command line.

del3rd yourTestFile.txt

Code: [Select]@echo off
for /f %%x in ('type %1 ^| find "[@##_^ stupid_line ^[emailprotected]#@]" /v /c') do set /A z=%%x - 2
sort /r /+1000000 %1 | more +3 | sort /r /+1000000 | more +1 > tmp
more +%z% %1 >> tmp
del %1
ren tmp %1

I wrote it for W2000 (I haven't installed XP) and it work fine if the file:

--> have not any line with the string: "[@##_^ stupid_line ^[emailprotected]#@]"
--> Haven't any line of 1000000 characters.

If it works for you, then you can write a 'FOR loop' for all your 6000 files.

Bye - Good luck



Discussion

No Comment Found