Sign in to follow this  
Followers 0
viper67

compare 2 txt files. how ?

8 posts in this topic

hallo.

i like to compare 2 txt files, but it doesn't work. can anyone help me ?

here's my script.

-------------------------------

$file1 = FileOpen("test.txt", 0)

$file2 = FileOpen("test2.txt", 0)

; Check if file opened for reading OK

If $file1 = -1 Then

MsgBox(0, "Error", "File1 kann nicht geöffnet werden.")

Exit

EndIf

If $file2 = -1 Then

MsgBox(0, "Error", "File2 kann nicht geöffnet werden.")

Exit

EndIf

; Read in 1 character at a time until the EOF is reached

While 1

$chars1 = FileRead($file1, 1000000)

If @error = -1 Then ExitLoop

MsgBox(0, "Inhalt 1:", $chars1)

$chars2 = FileRead($file2, 1000000)

If @error = -1 Then ExitLoop

MsgBox(0, "Inhalt 2:", $chars2)

Wend

If ($chars1 = $chars2) Then

MSgBox (0, "Vergleich", "Der Text hat sich nicht geändert.")

Else

MSgBox (0, "Vergleich", "Der Text hat sich geändert.")

EndIf

FileClose($file1)

FileClose($file2)

-------------------------------

Thanx for your support

viper67

Share this post


Link to post
Share on other sites



why not use FC ?

Andre


What about Windows without using AutoIt ?It would be the same as driving a car without an steering Wheel!

Share this post


Link to post
Share on other sites

Okay you are wanting to see if the contents are exactly the same as in character per character. Then what I would do is the following.

I just created a UDF that will compare files. I hope to get it included in the next release of AutoIt. Let me know how it works for you. You can also view it and any changes that may be made to it at _FileCompare($file1, $file2)

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

An example of how to use it.

$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))

I hope this helps you out.

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

Small question: I carried out the function and despite everything I still have a return -1... The files are existing and different. Why ?


----------------------GroumphyMore information about me [Fr]

Share this post


Link to post
Share on other sites

Hallo JSThePatriot

Thats it. It works :(

Thanx for your help.

viper67

Okay you are wanting to see if the contents are exactly the same as in character per character. Then what I would do is the following.

I just created a UDF that will compare files. I hope to get it included in the next release of AutoIt. Let me know how it works for you. You can also view it and any changes that may be made to it at _FileCompare($file1, $file2)

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

An example of how to use it.

$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))

I hope this helps you out.

JS

<{POST_SNAPBACK}>

Share this post


Link to post
Share on other sites

Small question: I carried out the function and despite everything I still have a return -1... The files are existing and different. Why ?

<{POST_SNAPBACK}>

Could you show me the code you are using to call the function? I will see if I can debug it. Are you sure the files are the exact same char by char?

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

Hallo JSThePatriot

Thats it. It works :(

Thanx for your help.

viper67

<{POST_SNAPBACK}>

I am glad I was able to help. In the scripts and scraps section I am soon going to add the functionality of comparing by size and such.

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