What I’m trying to do is a write a script to compare sections from two text files, and from that comparison create a third text file listing the differences. The first file is a constant text file I keep with the script. I want the script to prompt for the second file – I’m using Set ObjFSO = CreateObject("UserAccounts.CommonDialog") to accomplish that.
The files each have multiple sections – sort of like this:
[Application]
Application1=
ApplicationABC,Version1.234,Local Install,-,-,Y,-,Y,-,SDLIB,-,-,-,-
Application2=
ApplicationXYZ,Version15,Local Install,-,-,Y,-,N,-,SDLIB,-,-,-,-
[InstalledApplication]
NextInstalledApplication=
[MachineType]
MachineType2=Laptop,-,-,-,Laptop
I only need to compare the [Application] sections of each file, and only care about the Application Name and Version (in bold above) – everything else is irrelevant.
The problem comes in two places
- The sections I want to compare are not necessarily in the same order within the files.
- The application lines themselves vary somewhat – For example, MS Office could be Application25 in one file, or Application30 in another. Some of the info following the name and version can also vary so I can’t just compare line by line.
This is what I have so far – very rough – after working out the details, I also want to look into picking multiple files, where the script compares, and creates a differences document for each. Also, getting the differences document to use the name of the original user-chose document would be nice.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("C:\AmFam\Test\AOEPC.txt", ForReading)
Set Obj2FSO = CreateObject("UserAccounts.CommonDialog")
Obj2FSO.Filter = "VBScripts|*.vbs|Text Files|*.txt|All Files|*.*"
Obj2FSO.FilterIndex = 3
Obj2FSO.InitialDir = "\\Sofenterpriseserver.amfam.com\Sofstage$\Prod\Generic\"
'Read first file
strApplications = objFile1.ReadAll
objFile1.Close
'Prompt for second file
InitFSO = Obj2FSO.ShowOpen
If InitFSO = False Then
Wscript.Echo "Script Error: Please select a file!"
Wscript.Quit
Else
WScript.Echo "You selected the file: " & Obj2FSO.FileName
End If
'Read second file
Set objFile2 = objFSO.OpenTextFile(Obj2FSO.FileName, ForReading)
'Compare files
Do Until objFile2.AtEndOfStream
strCurrent = objFile2.ReadLine
If InStr(strApplications, strCurrent) = 0 Then
strDifference = strDifference & strCurrent & vbCrLf
End If
Loop
objFile2.Close
'Display differences
Wscript.Echo "Differences: " & vbCrLf & strDifference
'Save differences to a text file
Set objFile3 = objFSO.CreateTextFile("C:\AmFam\Test\Differences.txt")
objFile3.WriteLine strDifference
objFile3.Close