Jump to content
Sign in to follow this  
ennis

cant compare strings properly

Recommended Posts

ennis

Heres a function that compares 2 txt files to see if they match. If not it outputs the line that doesnt. The section that compares the 2 strings doesnt work( if $line1 <> $line2 then.....)...Im not sure why..It seems simple enough..But heres to fresh pair of eyes..I'll update this for others once it works as i had trouble finding one that does the job.

Thanks. Ennis

Func compare($file1, $file2)
;check if 2 passed files exist
    if not fileexists($file1) then
        msgbox(4096, "Error", $file1 & " does not exist!")
        return
    endif
    if not fileexists($file2) then
        msgbox(4096, "Error", $file2 & " does not exist!")
        return
    endif
;open files in read mode
    Local $handle1 = fileopen($file1, 0)
    Local $handle2 = fileopen($file2, 0)
;store txt lines read
    Local $line1 = ""
    Local $line2 = ""
;have 2 counts to record current line number
    Local $linecount1 = 1
    Local $linecount2 = 1
;2 vars to store @ error for each file read
    Local $error1 = 0
    Local $error2 = 0
;loop  1
    While 1
        $line1 = filereadline($handle1, $linecount1)
        $error1 = @error
        $line2 = filereadline($handle2, $linecount2)
        $error2 = @error
   ;if end of 1 reached --check eof 2 --if both yes then exit ---else quit say one file ended other file not at line count..print last line of not ended.
        select
            case $error1 = 1
                msgbox(4096, "Error", $file1 & " open error! Quitting compare()")
                Return
            case $error2 = 1
                msgbox(4096, "Error", $file2 & " open error! Quitting compare()")
                return
            case $error1 = -1 and $error2 = -1
                EXITLOOP
            case $error1 = -1 and $error2 <> -1
                msgbox(4096, "Error", "Files do not match!")
                msgbox(4096, "Error", $file1 & ": EOF reached")
                msgbox(4096, "Error", $file2 & ": Still on line " & $linecount2)
                msgbox(4096, "Error", "Line: " & $line2)
                return
            case $error2 = -1 and $error1 <> -1
                msgbox(4096, "Error", "Files do not match!")
                msgbox(4096, "Error", $file2 & ": EOF reached")
                msgbox(4096, "Error", $file1 & ": Still on line " & $linecount1)
                msgbox(4096, "Error", "Line: " & $line1)
                return
        Endselect
        if $line1 <> $line2 then
            msgbox(4096, "Error", "Files do not match at Line " & $line1 & " !")
            msgbox(4096, "Error", $file1 & " Line:")
            msgbox(4096, "Error", $line1)
            msgbox(4096, "Error", $file2 & " Line:")
            msgbox(4096, "Error", $line2)
        ENDIF
        $linecount1 = $linecount1 + 1
        $linecount2 = $linecount2 + 1
    WEND
    msgbox(4096, "Event", "EOF reached for both files. Compare completed successfully!")
ENDFUNC
Edited by ennis

Share this post


Link to post
Share on other sites
Jos

The section that compares the 2 strings doesnt work( if $line1 <> $line2 then.....)...Im not sure why..It seems simple enough..But heres to fresh pair of eyes..I'll update this for others once it works as i had trouble finding one that does the job.

<{POST_SNAPBACK}>

works ok here ... What is the issue ?

Can you attach a couple of records of the 2 files you have an issue with..


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ennis

i will attach some file tommorow..but it doesnt matter which files are used. It never says theres an error. So im assuming its not working. I took a registry export txt file, made a copy and added some random text in one of them...It tells me they both match..which they dont.

Share this post


Link to post
Share on other sites
Jos

i will attach some file tommorow..but it doesnt matter which files are used. It never says theres an error. So im assuming its not working. I took a registry export txt file, made a copy and added some random text in one of them...It tells me they both match..which they dont.

<{POST_SNAPBACK}>

try 2 test text files with a couple of records... they work fine.

It could be you have a UNICODE file which reports EOF too soon because it contains NULL characters

Try converting it first with the dos command TYPE.. see HERE


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ennis

youre right actually they are all unicode..didnt know about that..will try it out..Thanks

*Works Thank you

Ennis

Edited by ennis

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×