Micro Focus QTP (UFT) Forums
Please resolve this issue in comparing 2 text files - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Please resolve this issue in comparing 2 text files (/Thread-Please-resolve-this-issue-in-comparing-2-text-files)



Please resolve this issue in comparing 2 text files - kkishore12345 - 01-11-2009

I have written the following function to compare 2 text files.

Code:
Public Function CompareFiles2 (FilePath1, FilePath2)
Dim FS, File1, File2
Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.GetFile(FilePath1).Size <> FSO.GetFile(FilePath2).Size Then
CompareFiles2 = True
Exit Function
End If

Set File1 = FSO.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FSO.GetFile(FilePath2).OpenAsTextStream(1, 0)

CompareFiles2 = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read(1000)
Str2 = File2.Read(1000)

CompareFiles2 = StrComp(Str1, Str2, 0)

If CompareFiles2 <> 0 Then
CompareFiles2 = True
Exit Do
End If
Loop

File1.Close()
File2.Close()
End Function

In the Main action, I have written the following code:

Code:
FilePath1="C:\Temp\" & DataTable("FileName",dtGlobalSheet) & "_" & DataTable("GLang",dtGlobalSheet) & "_" & "privacy" & "_" & "Actual" & ".txt"
FilePath2= "C:\Temp\" & DataTable("FileName",dtGlobalSheet) & "_" & DataTable("GLang",dtGlobalSheet) & "_" & "termsandprivacy" & ".txt"

If CompareFiles2(FilePath1,FilePath2) = False Then
Reporter.ReportEvent micPass, "Files are same","Passed"
Else
Reporter.ReportEvent micFail, "Files are different","Fail"
End If
I am not getting the result, getting error in function.

Please resolve this issue ASAP.
Kishore


RE: Please resolve this issue in comparing 2 text files - kkishore12345 - 01-11-2009

I tried with the following function

Code:
Dim fso, MyFile1, MyFile2, comp, ln1, ln2
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile1 = fso.OpenTextFile(path & "\\" & file1, ForReading)
Set MyFile2 = fso.OpenTextFile(path & "\\" & file2, ForReading)
Do While ((MyFile1.AtEndOfStream <> True) OR (MyFile2.AtEndOfStream <> True))
ln1 = MyFile1.ReadLine
If ln1 <> "" Then
reporter.ReportEvent micPass,"File1 reading is successful",ln1
else
reporter.ReportEvent micFail,"File1 reading is NOT successful","fail"
End If
ln2 = MyFile2.ReadLine
If ln2 <> "" Then
reporter.ReportEvent micPass,"File2 reading is successful",ln2
else
reporter.ReportEvent micFail,"File2 reading is NOT successful","fail"
End If
comp = strcomp( ln1, ln2, BinaryCompare)
if (comp <> 0) then
MyFile1.Close
MyFile2.Close
CompareFile = 1
Exit Function
end if
Loop

MyFile1.Close
MyFile2.Close

CompareFile = 0
End Function

In the Main action, I wrote the following code
Code:
path="C:\Temp\"
file1=DataTable("FileName",dtGlobalSheet) & "_" & DataTable("GLang",dtGlobalSheet) & "_" & "privacy" & "_" & "Actual" & ".txt"
file2=DataTable("FileName",dtGlobalSheet) & "_" & DataTable("GLang",dtGlobalSheet) & "_" & "termsandprivacy" & ".txt"


If CompareFile(path,file1,file2)=1 Then
Reporter.ReportEvent micPass, "Files are same","Passed"
Else
Reporter.ReportEvent micFail, "Files are different","Fail"
End If
When I run this test, the test is getting passed. However, as I written the code to print the ln1 and ln2 which means readline from file1 and file2, in the results window, the information present in the attached gif file is displayed.

[quote=kkishore12345]
I have written the following function to compare 2 text files.

Public Function CompareFiles2 (FilePath1, FilePath2)
Dim FS, File1, File2
Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.GetFile(FilePath1).Size <> FSO.GetFile(FilePath2).Size Then
CompareFiles2 = True
Exit Function
End If

Set File1 = FSO.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FSO.GetFile(FilePath2).OpenAsTextStream(1, 0)

CompareFiles2 = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read(1000)
Str2 = File2.Read(1000)

CompareFiles2 = StrComp(Str1, Str2, 0)

If CompareFiles2 <> 0 Then
CompareFiles2 = True
Exit Do
End If
Loop

File1.Close()
File2.Close()
End Function

In the Main action, I have written the following code:

FilePath1="C:\Temp\" & DataTable("FileName",dtGlobalSheet) & "_" & DataTable("GLang",dtGlobalSheet) & "_" & "privacy" & "_" & "Actual" & ".txt"
FilePath2= "C:\Temp\" & DataTable("FileName",dtGlobalSheet) & "_" & DataTable("GLang",dtGlobalSheet) & "_" & "termsandprivacy" & ".txt"

If CompareFiles2(FilePath1,FilePath2) = False Then
Reporter.ReportEvent micPass, "Files are same","Passed"
Else
Reporter.ReportEvent micFail, "Files are different","Fail"
End If

I am not getting the result, getting error in function.

Please resolve this issue ASAP.
Kishore