Q. How can I compare two files line by line?

Jerold Schulman

November 21, 2006

1 Min Read
ITPro Today logo

I have scripted FileCompare.vbs to perform a case insensitive comparison of two files, line by line. The script will list the lines in file1 that are not in file2, and the lines in file2 that are not in file1.

The syntax for using FileCompare.vbs is:

Cscript //nologo FolderFileCompare.vbs FQFN1 FQF2

Where FQFN1 is the fully qualified name of file1 and FQFN2 is the fully qualified name of file2.

FileCompare.vbs contains:

Dim objASet objA = Wscript.Argumentsif objA.count  2 Then    Wscript.Echo "FileCompare requires File1 and File2 arguments."     Wscript.QuitEnd IfSet objFSO = CreateObject("Scripting.FileSystemObject")Set objF1 = objFSO.OpenTextFile(objA(0), 1)Set objF2 = objFSO.OpenTextFile(objA(1), 1)Set objL1 = CreateObject("Scripting.Dictionary")objL1.CompareMode = vbTextCompareSet objL2 = CreateObject("Scripting.Dictionary")objL2.CompareMode = vbTextCompare' Read first file adding unique value to dictionary object.Do Until objF1.AtEndOfStream    strV = objF1.ReadLine    If (objL1.Exists(strV) = False) Then        objL1.Add strV, True    End IfLoopobjF1.Close' Read the second file.Wscript.Echo "The following values are only in " & objA(1) & "."Do Until objF2.AtEndOfStream    strV = objF2.ReadLine    If (objL1.Exists(strV) = False) And (objL2.Exists(strV) = False) Then        Wscript.Echo strV    End If' Remove duplicates.    If (objL1.Exists(strV) = True) Then        objL1.Remove strV    End If' Add unique values to 2nd dictionary object.    If (objL2.Exists(strV) = False) Then        objL2.Add strV, True    End IfLoopobjF2.CloseWscript.Echo "The following values are only in " & objA(0) & "."arrL1 = objL1.KeysFor Each strV In arrL1    Wscript.Echo strVNext


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like