Comparing 2 Text Files Using Vbscript/QTP

How to compare 2 text files

The following function can be used to check and compare the content of the two files.
This function is not part of QuickTest Professional/Astra QuickTest. It is not guaranteed to work and is not supported by Mercury Interactive Technical Support. You are responsible for any and all modifications that may be required.
CompareFiles (FilePath1, FilePath2)
The path to the first file to compare
The path to the second file to compare
Public Function CompareFiles (FilePath1, FilePath2)
Dim FS, File1, File2
Set FS = CreateObject(“Scripting.FileSystemObject”)
If FS.GetFile(FilePath1).Size FS.GetFile(FilePath2).Size Then
CompareFiles = True
Exit Function
End If
Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
CompareFiles = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read(1000)
Str2 = File2.Read(1000)
CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles 0 Then
CompareFiles = True
Exit Do
End If
End Function
Return value:
The function returns 0 or False if the two files are identical, otherwise True.
File1 = “C:\Text1.txt”
File2 = “C:\Text2.txt”
If CompareFiles(File1, File2) = False Then
MsgBox “Files are identical.”
MsgBox “Files are different.”
End If


Popular posts from this blog

Testing Webservice with SoapUI Having Windows Authentication

How to Re-run Failed Scenarios in Cucumber

Data Driven Testing Using Selenium -WebDriver in Python