That deleted the strings and the VBCRLF, but not the entire line.
Main Topics
Browse All TopicsI am trying to write a VBS that will remove a line from a text file if it contains a certain string value. I've got most of the way through, except for the part about deleting the line. That's where I'm stuck. The VBS will read all text files in the current directory, and remove lines from the text files based on a string comparison.
I've looked up some solutions to deleting a line from a text file, or replacing strings. They all: opend the file, read the lines with a comparison, concantenated a string, closed the file, opened the file for writing and wrote the concantenated string. I understand that this is very slow and I don't want to do this. Currently, just the below code takes about 30 seconds to run. I have lots of text files and the smaller ones have around 50,000 lines (though mostly made up of the line I'm trying to remove).
How do I delete a line? That's the only part I'm missing.
Option Explicit
dim filepath, VBSName, objFSO, objTextFile
dim fs, folder, file, item, line
VBSname = wscript.scriptfullname
Filepath = left(VBSname, Len(VBSname) -(len(VBSname) - InstrRev(VBSname, "\")))
set fs = CreateObject("Scripting.Fi
set folder = fs.GetFolder(FilePath)
for each item in folder.Files
If Right(item.name, 3) = "txt" Then
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile (item, 1)
Do Until objTextFile.AtEndOfStream
Line = objTextFile.ReadLine
If InStr(1,Line, " Host Domain: ",1) Then
Else
End If
Loop
set objFSO = nothing
set objTextFile = nothing
End If
next
msgbox "Done!"
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
There are two only two basic ways to delete a line from a text file:
(1) Read the contents into memory. Modify the contents in memory. Overwite the file with the new contents.
(2) Read the file line by line. Echo only the desired lines to a secondary file. Delete the original primary file. Rename the secondary file to the primary file.
VB is very slow at string manipulation so method (1) is going to be slow with large files. Try method (2) and see if it's any faster.
I don't know what kind of machine you are running it at but I ran this on a file containing 100,000 lines and it completed in 3 seconds. I have a good machine but yours can't be that bad on any kind of machine. Also disk i/o is much slower compared to in memory comparision in any language. Thus reading and writing line by line would be much slower. Only when the RAM is running low, the in memory reading of file would translate to swap file writing and may cause the performace to degrade.
I noticed that the pattern still has an extra space in it. It should be
oRegExp.Pattern = ".*Host Domain:.*\n"
I went with this. It works fine and takes less than a second to run through all my files.
Option Explicit
dim filepath, VBSName, objFSO, objTextFile, objTextFile2
dim fs, folder, file, item, line, fs1, filename
VBSname = wscript.scriptfullname
Filepath = left(VBSname, Len(VBSname) -(len(VBSname) - InstrRev(VBSname, "\")))
set fs = CreateObject("Scripting.Fi
set folder = fs.GetFolder(FilePath)
For each item in folder.Files
If Right(item.name, 3) = "txt" Then
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile (item, 1)
Set fs1 = objFSO.CreateTextFile ("temp.txt",True)
fs1.close
Set objTextFile2 = objFSO.OpenTextFile ("temp.txt", 8)
Do Until objTextFile.AtEndOfStream
Line = objTextFile.ReadLine
If InStr(1,Line, " Host Domain: ",1) Then
else
objTextFile2.writeline line
End If
Loop
set objTextFile = nothing
set objTextFile2 = nothing
filename = item.name
objFSO.CopyFile "temp.txt", filename, true
objFSO.DeleteFile "temp.txt"
set objFSO = nothing
Set fs1 = nothing
End If
next
msgbox "Done!"
The ReadAll should also take about the same (acuallty less but you probably won't be able to notice). I have done similar things several times and ReadAll has always performed better than reading line by line. In any case it can't take 15 minutes and still be not over. Something else must have happened at that time due to which either the script ran so slow or for some reason it might have hanged.
amit_g,
You are correct in saying that memory access is faster than hard drive access.
But vb is very slow at string manipulation. If you use .ReadAll on a large file and then manipulate the resulting string, VB then has two make a copy in memory of the entire string. Depending on what the system is doing this may take quite awhile.
Reading line by line may be slower for small files (and probably slower in general), but may make sense for larger files since it only has to deal with one line at a time. It's a trade off...
Business Accounts
Answer for Membership
by: amit_gPosted on 2006-01-13 at 17:53:10ID: 15697997
This is probably as fast as it can get with VBScript ...
leSystemOb ject")
leSystemOb ject")
Option Explicit
dim filepath, VBSName, objFSO, objTextFile
dim fs, folder, file, item, Body
Dim oRegExp
VBSname = wscript.scriptfullname
Filepath = left(VBSname, Len(VBSname) -(len(VBSname) - InstrRev(VBSname, "\")))
Set objFSO = CreateObject("Scripting.Fi
set fs = CreateObject("Scripting.Fi
set folder = fs.GetFolder(FilePath)
Set oRegExp = New RegExp
oRegExp.Pattern = " Host Domain: .*\n"
oRegExp.IgnoreCase = True
oRegExp.Global = True
oRegExp.MultiLine = True
for each item in folder.Files
If Right(item.name, 3) = "txt" Then
Set objTextFile = objFSO.OpenTextFile (item, 1)
Body = objTextFile.ReadAll
set objTextFile = nothing
Body = oRegExp.Replace(Body, "")
Set objTextFile = objFSO.CreateTextFile (item, True)
Call objTextFile.Write(Body)
set objTextFile = nothing
End If
next
set objFSO = nothing
msgbox "Done!"