QTP tutorials & interview questions


Search more on QTP here

QTP Script 35 - How to compare two files in QTP?

Public Function CompareFiles (F_Path_1, F_Path_2)

Dim FS, first_file, second_file
Set FS = CreateObject("Scripting.FileSystemObject")

'Function will simply exit if the size of both files being
compared is not equal


If FS.GetFile(F_Path_1).Size <> FS.GetFile(F_Path_2).Size Then
CompareFiles = True
Exit Function
End If

'OpenAsTextStream Method: Opens a specified file and returns
a TextStream object that can be used to read from, write to,
or append to the file.


Set first_file = FS.GetFile(F_Path_1).OpenAsTextStream(1, 0)
Set second_file = FS.GetFile(F_Path_2).OpenAsTextStream(1, 0)

CompareFiles = False

'AtEndOfStream Property: Read-only property that returns True
if the file pointer is at the end of a TextStream file; False
if it is not.


Do While first_file.AtEndOfStream = False

'The Read method reads a specified number of characters from
a TextStream file


Str1 = first_file.Read(1000)
Str2 = second_file.Read(1000)

'The StrComp function compares two strings and returns 0 if
the strings are equal


CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles <> 0 Then
CompareFiles = True
Exit Do
End If
Loop

first_file.Close()
second_file.Close()
End Function

File1 =inputbox ("Enter first file name e.g. C:\Readme1.pdf")
File2 = inputbox ("Enter second file name e.g. C:\Readme2.pdf")

If CompareFiles(File1, File2) = False Then
MsgBox File1 & " and " & File2 & " " & "are identical."
Else
MsgBox File1 & " and " & File2 & " " & "are NOT identical."
End If