Tuesday, December 2, 2008

Compare two text files using standart Windows utility

I faced situation when in my QTP test I need compare two html pages to find out what changed in this HTML code.
I searched some time in Google and tried to find some tool with command line or ActiveX interface,
so I can use it for my test needs. But my efforts did not give acceptable results.
Finally, I decided to use standard Windows utility named fc.exe (abbreviation from 'File Compare').
This utility needs as arguments path to two text files to compare and returns text report with comparison result.
This function below returns result of comparison in text format.
Maybe it helps someone...


'Function that returns True if both text files are the same, returns False if something goes wrong during function execution, and returns list of differences in text format if files are different
'Arguments:
'firstFile
'secondFile
Function compareFiles(firstFile, secondFile)
compareFiles = False
resultOutput = ""
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FilesystemObject")
If fso.FileExists(firstFile) Then
If fso.FileExists(secondFile) Then
Set WshExec = WshShell.Exec("%windir%\system32\fc.exe """& secondFile &""" """& firstFile & """")
Set oTextStream = WshExec.StdOut
text = oTextStream.ReadAll
If InStr(1, text, "FC: no differences encountered", 1) = 0 Then
compareFiles = text
Else
compareFiles = True
End If
'Wait until WshExec terminated
Do Until WshExec.Status = 1
Wait 0.1
Loop
Set WshExec = Nothing
End If
End If
End Function

curr = "E:\BDWizReg.log"
prev = "E:\Copy of BDWizReg.log"

msgbox(compareFiles(curr, prev))
Powered By Blogger