deleting multiple lines in multiple text file

Dell / OPTIPLEX 210L
August 20, 2009 at 10:34:34
Specs: Microsoft Windows XP Professional, 2.992 GHz / 1014 MB

I need to delete multiple lines in multiple text files.

Lines Text
1 Test
2 Test
3 Test
4 Test
5 Begin

So anything above line 5 (i.e. begin) needs to be deleted. I have searched the web and found it is possible when i do it using VB script, but the draw back is i can only do one text file at a time. The Source code for VB is as follows:

Const ForReading = 1
Const ForWriting = 2

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile('C:\testing\testing.txt', ForReading)

strContents = objFile.ReadAll

arrLines = Split(strContents, vbCrLf)

Set objFile = objFSO.OpenTextFile(C:\testing\testing.txt', ForWriting)

For i = 5 to UBound(arrLines)
objFile.WriteLine arrLines(i)

Question: How do i specify multiple fies in the
Set objFile = objFSO.OpenTextFile(C:\testing\testing.txt', ForWriting) line....coz if use wildcards the script does not undersatnd it...example ..*.txt

Is there a way to do it in batch programming?

Any help would be greatly appreciated.

Thanks in advance.

See More: deleting multiple lines in multiple text file

Report •

August 20, 2009 at 12:14:15
Hmm. Now i don't know about the deleting everything above line 5 bit. I currently know how to delete 1 line off many text files though.
I know how to delete multiple lines too, but it's an awkward way.
Set a user input variable for the search like this.
SET /p %search%=Search:
Ok, now it's asking for you to search something.
Now heres the code that deletes all the lines that contain that word you typed.

FINDSTR /v /i "%search%" < Database.txt > Database2.txt
DEL Database.txt
REN Database2.txt Database.txt

Remember, Database.txt is from my batch, so you will have to put your own file there that you want to search. You export the new text (without the lines containing the word you searched) to a new txt file called Database2.txt or whatever you put that as. It then deletes the first txt file then renames the database2 file back to its original name. Thats slightly confusing, but thats how it works.

To do multiple text files, you basically copy the FINDSTR line a few more times underneath, then edit the file names. and it will do them all one after another.

Theres 1 very bad thing about this. Say for instance you search "fred" and theres many lines in the text file that contain the word fred. You will delete all of those lines.
Also, if you put a Letter A, any line with that letter in will delete..
so make sure you type in what you want.

Sorry this is confusing, I'm a confusing person, I never make sense. But thats how i'd do it anyways. There may be a better way but I'm just not as expert as some of the guys here.

Need BatchFile Help ?
Msg me :)

Report •

August 21, 2009 at 01:46:11
@echo off > newfile & setLocal enableDELAYedexpansion

for /f "skip=4 tokens=* delims= " %%a in (myfile) do (
echo %%a >> newfile

If at first you don't succeed, you're about average.


Report •

August 21, 2009 at 04:44:05
Thank you so much for your response, but your code sure does help when i have less files. But not much when i have huge volume of files.

Thank you for your response...I see that your script ignores the first four lines and then copies the rest to a new file. That is one way of doing it & i must thank you for your script. But in this case i need to do it without creating a new file and that leaves me with just editing the original file.

I guess some minor tweak to your script should do the task....please advise..


Report •

Related Solutions

August 21, 2009 at 06:15:44
It's usually a bad idea to 'edit' a file with a batch.

If at first you don't succeed, you're about average.


Report •

Ask Question