I 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
leSystemOb
ject")
set folder = fs.GetFolder(FilePath)
for each item in folder.Files
If Right(item.name, 3) = "txt" Then
Set objFSO = CreateObject("Scripting.Fi
leSystemOb
ject")
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!"
Start Free Trial