Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function for comaprison of contents of two text files
#1
Solved: 10 Years, 7 Months, 3 Weeks ago
Hi Everyone,

My Greetings.
I have an Issue.I have to COMPARE the contents of two text files.
For your kind reference i have written my piece of code here :
------------------------------------------------------------
Code:
Const ForReading = 1, ForWriting = 2
Dim fso, txtFile, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile1 = fso.OpenTextFile("C:\CurrentDIR.txt", ForReading)
Set f = fso.OpenTextFile("C:\Files2Download.txt", ForWriting, True)

Do Until txtFile1.AtEndOfStream
strMatch = False
    strLine1 = txtFile1.Readline
Set txtFile2 = fso.OpenTextFile("C:\LOG.txt", ForReading)
        Do Until txtFile2.AtEndOfStream
            strLine2 = txtFile2.Readline
                If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
                    strMatch = True
                Else  
                End If    
        Loop
        txtFile2.Close
                If strMatch <> True then
                    f.writeline strLine1
                End If
Loop
f.Close
Wscript.Echo "Done"
------------------------------------------------------------

This works fine.
But,I have two/three conditions to be met here.
1. I want the textfiles NOT to be hardcoded.
2. I want this to be written as a function.
3. Also, my First file is static ie the first file is the expected result. My Other two files are generated during runtime. ie the 2nd file which would store Actual result and the 3rd file which would be the differences.


Like if my QAManager writes like this , it should work.
Call FnComptext(tf1,tf2).
It should compare and store the diff in another file.

Plz Help.
Thanks a ton,
Josh Q
Reply
#2
Solved: 10 Years, 7 Months, 3 Weeks ago
If I understood your question correctly, you want to compare two files CurrentDIR.txt and LOG.txt and store their differences in a file name Files2Download.txt

Here is how you can convert it into a function.

Code:
Function Comp (text_file1, text_file2, diff_file)

Const ForReading = 1, ForWriting = 2
Dim fso, txtFile, txtFile2, strLine1, strLine2, strMatch
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile1 = fso.OpenTextFile("text_file1", ForReading)
Set f = fso.OpenTextFile("diff_file", ForWriting, True)

Do Until txtFile1.AtEndOfStream
strMatch = False
strLine1 = txtFile1.Readline
Set txtFile2 = fso.OpenTextFile("text_file2", ForReading)
Do Until txtFile2.AtEndOfStream
strLine2 = txtFile2.Readline
If Trim(UCase(strLine2)) = Trim(UCase(strLine1)) Then
strMatch = True
Else
End If
Loop
txtFile2.Close
If strMatch <> True then
f.writeline strLine1
End If
Loop
f.Close
Wscript.Echo "Done"

End Function


you can call this function by :

Comp C:\CurrentDIR.txt, C:\LOG.txt, C:\Files2Download.txt
Want to fast track your QTP/UFT Learning? Join our UFT Training Course
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling a Function in Function Library when function is defined in an Action jitenderkkr 0 2,750 11-27-2014, 12:53 PM
Last Post: jitenderkkr
  Read text from text file and save it into a variable in qtp arpan 3 12,016 06-19-2013, 08:34 PM
Last Post: arpan
  Need to compare two text files - ignoring some text nelly27281 2 4,346 09-09-2012, 12:09 PM
Last Post: freeboynil
Exclamation How to read the contents of external excel sheet qtplearner88 10 10,157 05-25-2012, 12:00 PM
Last Post: supputuri
  Finding specific text in a text string janriis 3 5,170 10-08-2010, 04:00 PM
Last Post: KavitaPriyaCR

Forum Jump:


Users browsing this thread: 1 Guest(s)