Sign in to follow this  
Followers 0
JSThePatriot

_FileCompare($file1,$file2)

10 posts in this topic

#1 ·  Posted (edited)

I saw someone in the support forum needing this. I have seen this a few different times being needed so I decided to create a UDF for it.

The code:

Func _FileCompare($file1 = "", $file2 = "")
    Local $file1, $file2;Files/Paths
    Local $o_file1, $o_file2;Open file handles
    Local $chars1, $chars2;Number of Chars in file according to number (ex. $chars1 holds #chars in $file1)
    Local $chars;Initial variable for each character count.
    
;Checks to be sure that both files were set parameters.
    If $file1 = "" OR $file2 = "" Then
        SetError(1)
        Return -1
    EndIf
    
;Assumes if the path isnt provided it is in the script directory.
    If Not(StringInStr($file1, "\")) Then
        $file1 = @ScriptDir & "\" & $file1
    EndIf
    
;Assumes if the path isnt provided it is in the script directory.
    If Not(StringInStr($file2, "\")) Then
        $file2 = @ScriptDir & "\" & $file2
    EndIf
    
;Open file and check to be sure it opened.
    $o_file1 = FileOpen($file1, 0)
    If $o_file1 = -1 Then
        MsgBox(0, "Unable to Open File", "Unable to open file: " & $file1)
        SetError(1)
        Return -1
    EndIf
    
;Open file and check to be sure it opened.
    $o_file2 = FileOpen($file2, 0)
    If $o_file2 = -1 Then
        MsgBox(0, "Unable to Open File", "Unable to open file: " & $file2)
        SetError(1)
        Return -1
    EndIf

;Go through the file and read the whole thing into a variable
    While 1
        $chars = FileRead($o_file1, 1)
        If @error = -1 Then ExitLoop
        $chars1 = $chars1 & $chars
    WEnd

;Go through the file and read the whole thing into a variable
    While 1
        $chars = FileRead($o_file2, 1)
        If @error = -1 Then ExitLoop
        $chars2 = $chars2 & $chars
    WEnd

    FileClose($o_file1)
    FileClose($o_file2)
    
    If ($chars1 == $chars2) Then
        Return 1
    Else
        Return -1
    EndIf
EndFunc

I have thought about adding different ways to compare the files. Like compare size would be _FileCompare($file1, $file2, 1) Let me know if anyone has any ideas or comments on this. Or also if the code could be optimized.

Edit: Example use:

$file1 = "swfout.txt"
$file2 = "C:\swfout.txt"

If _FileCompare($file1, $file2) Then
   ;do something here
EndIf

MsgBox(0, "Compare Files", "On success it will return 1 it returned: " & _FileCompare($file1, $file2))

Thanks,

JS

Edited by JSThePatriot

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites



;Assumes if the path isnt provided it is in the script directory.

If Not(StringInStr($file1, "\")) Then

$file1 = @ScriptDir & "\" & $file1

EndIf

;Assumes if the path isnt provided it is in the script directory.

If Not(StringInStr($file2, "\")) Then

$file2 = @ScriptDir & "\" & $file2

EndIf

This is uneeded. Autoit checks to see if it's in the script directory anyways.

Share this post


Link to post
Share on other sites

I did that so when I output the msgbox on failure to open the file that it would display the full path. I know its not needed and if there are enough people that dont want it in there I will remove.

What are your thoughts on my debugging method? (The whole reason I have that there) Not to mention I didnt know/think about the fact that AutoIt looked in the current directory, but now that you mention it, it rings a bell. Since you know my reason do you still think it should be removed?

Thanks for the comment,

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Why not replace this:

While 1
        $chars = FileRead($o_file1, 1)
        If @error = -1 Then ExitLoop
        $chars1 = $chars1 & $chars
   WEnd

with this?:

$chars = FileRead($o_file1, FileGetSize($o_File1))

that way it would read the whole file in one pass.

Edited by SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

Well if you're only using it to display the whole file path for debugging then it's fine.

Share this post


Link to post
Share on other sites

Why not replace this:   

While 1
        $chars = FileRead($o_file1, 1)
        If @error = -1 Then ExitLoop
        $chars1 = $chars1 & $chars
   WEnd

with this?:

$chars = FileRead($o_file1, FileGetSize($o_File1))

that way it would read the whole file in one pass.

<{POST_SNAPBACK}>

I originally had it coded that way, but I went ahead with this way incase a character by character comparison wanted to be made for what ever reason. I think I need to go with the modes. That way people can choose how they want it to work. Though the difference between the 2 ways we are talking about are trivial. I thank you for the input. I probably will change it to that way if there is no major difference between the 2 ways.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

In my opinion there are to many ways to compare files. You can compare an almost infinite number of ways by size, characters, first word, last word, 8th letter of the 115st word is the same as the 2nd letter of the 39th word. As you can see I'm just using them as an example to show how many ways you can infact compare them.

I believe when you need to compare files specifically you should create your own method/function but when you wish to compare the whole file or size then use the above.

Func _FileCompare($file1 = "", $file2 = "")
    Local $o_file1, $o_file2;Open file handles
    Local $chars1, $chars2
    Local $chars;Initial variable for each character count.
    $o_file1 = FileOpen($file1, 0)
    $o_file2 = FileOpen($file2, 0)
    $chars1 = FileRead($o_file1, FileGetSize($o_File1))
    $chars2 = FileRead($o_file2, FileGetSize($o_File2))
    FileClose($o_file1)
    FileClose($o_file2)
    If ($chars1 == $chars2) Then
        Return 1
    Else
        Return -1
    EndIf
EndFunc
Edited by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

In my opinion there are to many ways to compare files. You can compare an almost infinite number of ways by size, characters, first word, last word, 8th letter of the 115st word is the same as the 2nd letter of the 39th word. As you can see I'm just using them as an example to show how many ways you can infact compare them.

I believe when you need to compare files specifically you should create your own method/function but when you wish to compare the whole file or size then use the above.

Func _FileCompare($file1 = "", $file2 = "")
    Local $o_file1, $o_file2;Open file handles
    Local $chars1, $chars2
    Local $chars;Initial variable for each character count.
    $o_file1 = FileOpen($file1, 0)
    $o_file2 = FileOpen($file2, 0)
    $chars1 = FileRead($o_file1, FileGetSize($o_File1))
    $chars2 = FileRead($o_file2, FileGetSize($o_File2))
    FileClose($o_file1)
    FileClose($o_file2)
    If ($chars1 == $chars2) Then
        Return 1
    Else
        Return -1
    EndIf
EndFunc

<{POST_SNAPBACK}>

Yes there are an aweful lot of ways. That is true. But to compare some of the most common might be something worthwhile. Yes what you posted is the exact same thing I have except with out any error handling. If the files arent there then it wont work properly etc. You have a very valid point. I was wanting everyone to let me know if they thought this was a good idea and if there were any sort of File Comparisons they would like to see added. If it is just the 5 words 4th letter then that would be their issue. I was looking for common comparisons.

Now what would be good is if there was a fuction where you told it what you wanted to compare by. :( Not impossible. I have some first thoughts on it. Would take some time and patience. But it certainly could be done.

I really appreciate your taking the time to critique my code. :( I enjoy good iron sharpening iron.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

I originally had it coded that way, but I went ahead with this way incase a character by character comparison wanted to be made for what ever reason. I think I need to go with the modes. That way people can choose how they want it to work. Though the difference between the 2 ways we are talking about are trivial. I thank you for the input. I probably will change it to that way if there is no major difference between the 2 ways.

JS

<{POST_SNAPBACK}>

Anyway. Great work. This is a nice function to have around. now when i am comparing text files i won't have to use the FC Dos command in my scripts. :(

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

Anyway. Great work. This is a nice function to have around. now when i am comparing text files i won't have to use the FC Dos command in my scripts.  :(

<{POST_SNAPBACK}>

Thank you. I think I am going to add the functionality of a few other file comparisons that I can come up with.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

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  
Followers 0