Quick test professional

QTP Tips QTP codes QTP Faqs and more

compare 2 text files

Use the CompareFiles function
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:\countries\apple1.jpg”
File2 = “C:\countries\apple3.jpg”


If CompareFiles(File1, File2) = False Then
MsgBox “Files are identical.”
MsgBox “Files are different.”
End If

  Source: Mercury Forum’s KB articles


February 14, 2008 - Posted by | compare 2 text files | , , , , , , , ,


  1. Is it possible to compare two excel files at run time through QTP ?

    Comment by kishore | April 9, 2008 | Reply

  2. Yes you can do this with help of excel automation.
    Find some of the excel automation code in the same blog or open the following URL


    Comment by quicktestprofessional | April 9, 2008 | Reply

  3. Hi,

    Please help me to shortout below issue.

    I want to compare two text file line by line(unicode format). if any mismatch in the line it should write in another file and particular change text look should diffferent. It is easy to find out.

    Comment by Gopal Pattanik | March 2, 2011 | Reply

  4. Your referance is really useful and it work exectly the way i look for,but is there any way i can save difference between 2 notepad and save it on other notepad?


    Comment by Hari | May 12, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: