Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript File Comparison

Status
Not open for further replies.

Yaik

Programmer
Oct 1, 2010
36
US
So I am working on some code that will eliminate a word that is found on a text file.

For example, I have a file with some lines, and in one of those lines the word "Update" exists, it will remove that whole line. The words to remove is picked up from a text file with many words that I want to remove.

The code right now read the line from the "remove.txt" file and compares it to the "file.txt" file, and if the word is not found it writes that line to a variable. After the loop is done, it goes ahead and writes all the strings in the variable, then sets the variable to nothing, and goes to compare the second word in the "remove.txt" file.

The code somewhat works, but takes a long long time to go through. Can anyone help me make this code a little better.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set txtFile1 = objfso.OpenTextFile(".\remove.txt", ForReading)
Do Until txtFile1.AtEndOfStream

Set strLine1 = CreateObject("VBScript.RegExp")
    strLine1 = txtFile1.Readline
Set txtFile2 = objfso.OpenTextFile(".\file.txt", ForReading)
        Do Until txtFile2.AtEndOfStream
            strLine2 = txtFile2.Readline
			If InStr(strLine2, strLine1) = 0 Then
				
				strNewContents = strNewContents & strLine2 & vbCrLf
			End If
        Loop



txtFile2.Close
Set txtFile2 = objFSO.OpenTextFile(".\file.txt", ForWriting)
txtFile2.Write strNewContents

txtFile2.Close
strNewContents = nothing
Loop
txtFile1.Close
 
Rather than opening and closing 'textfile2' each time through the loop, you might consider holding the contents in an array and writing the array out to a file when you are done with the processing. Also, you are creating a regex object each time through the loop that never gets used, eliminate it.
 
Is this an extension of this thread: thread329-1622276?
 
Well, it is part of a much bigger script I am working on, but I prefer to work on it part by part. But thanks. I'll try and store it an array and see how that works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top