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 1

Status
Not open for further replies.

Yaik

Programmer
Oct 1, 2010
36
US
I am trying to make a VBScript that will take a text file and compare it with another.

The comparison is based in that, it will read a word in the first file, and if it finds that word on the second file, it will echo it.

My problem is that it only compares the first word of Track.txt to the other file, instead of going through the list of words I would like to find.

Any help is greatly appreciated.

Underneath is the script. I have two different ones, but they both do the same exact thing.

Track.txt is the list of words I want to find in SoftInv.txt. results.txt is where I am giong to output it.

Code:
Const ForReading = 1, ForWriting = 2
Dim fso, txtFile, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile1 = fso.OpenTextFile(".\Track.txt", ForReading)
Set strLine1 = CreateObject("VBScript.RegExp")


Do Until txtFile1.AtEndOfStream
strMatch = False
strLine1.Pattern = txtFile1.Readline
Set txtFile2 = fso.OpenTextFile(".\SoftInv4.txt", ForReading)
Do Until txtFile2.AtEndOfStream
strLine2 = txtFile2.Readline
Set colMatches = strLine1.Execute(strLine2) 
If colMatches.Count > 0 Then
For Each strMatch in colMatches 
Wscript.Echo strLine2 
Next
End If

Loop
f.Close
Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(".\SoftInv4.txt", ForReading)
Set objRegEx = CreateObject("VBScript.RegExp")

Set txtFile1 = objfso.OpenTextFile(".\Track.txt", ForReading)

Do Until txtFile1.AtEndOfStream
objRegEx.Pattern = txtFile1.Readline


Do Until objFile.AtEndOfStream
strSearchString = objFile.ReadLine
Set colMatches = objRegEx.Execute(strSearchString) 
If colMatches.Count > 0 Then
For Each strMatch in colMatches 
Wscript.Echo strSearchString 
Next
End If
Loop
Loop
objFile.Close
 
Try this:
Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(".\SoftInv4.txt", ForReading)
Set txtFile1 = objfso.OpenTextFile(".\Track.txt", ForReading)

Set objRegEx = new regexp
objRegEx.IgnoreCase = True
objRegEx.Global = True

contents = objFile.ReadAll
objFile.Close

Do Until txtFile1.AtEndOfStream
    objRegEx.Pattern = txtFile1.Readline

    Set colMatches = objRegEx.Execute(contents)
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches
            Wscript.Echo strMatch
        Next
    End If
Loop
 
Cool, that works, and I guess I didn't test it properly because mine did too, :/.

But ok, while we are at it, is there a way to make the script read the word from any part of the line it is looking at instead of the just the beginning. Let me explain myself.

Example

In track.txt I could have "ACCESS 2002 - XXTQ3" and on softinv.txt I have "Microsoft Access 2002 10.0.2627.01". But it doesn't find it. If I have "Microsoft Access" in track.txt, then it finds it.

It looks at the first word of track.txt and the first word of softinv.txt, and if they match it writes it down, if they don't then it doesn't.
 
You will need to use regular expressions as your pattern if you want to find variable text. Regular expressions do look at the entire line, but your example fails because "ACCESS 2002 - XXTQ3" doesn't appear exactly in what you are searching. If you want to find any version of access (2000 or newer) you could use "ACCESS 20\d\d" as your regex pattern. Are you trying to use XX as wildcards?

Some good info on regular expressions can be found at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top