Skip navigation

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

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 <Drive:>\Folder\FileCompare.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 objA
Set objA = Wscript.Arguments
if objA.count  2 Then
    Wscript.Echo "FileCompare requires File1 and File2 arguments." 
    Wscript.Quit
End If
Set 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 = vbTextCompare
Set 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 If
Loop
objF1.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 If
Loop
objF2.Close
Wscript.Echo "The following values are only in " & objA(0) & "."
arrL1 = objL1.Keys
For Each strV In arrL1
    Wscript.Echo strV
Next


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish