Sign in to follow this  
Followers 0
FreeSiker

Text file comparaison

11 posts in this topic

I'm trying to compare two .txt file and write the missing line in the other file, but the script detect only the first line

Do you have an idea ?

Thank you

While 1
    MsgBox(0, "Info", "Open transactmp.txt")
    ;Open transactmp.txt in read only
    $transactmp_read = FileOpen(@ScriptDir & "\transactmp.txt", 0)
    
    ;Verify if file is open
    If $transactmp_read = -1 Then
        MsgBox(0, "Error", "unable to open transactmp.txt")
        Exit
    EndIf

    MsgBox(0, "Info", "Opening emaillog.txt")
    ;Open emaillog.txt in read only
    $emaillog_read = FileOpen(@ScriptDir & "\emaillog.txt", 0)

    ;Verify if file is open
    If $emaillog_read = -1 Then
        MsgBox(0, "Error", "unable to open emaillog.txt")
        Exit
    EndIf
    
    While 1
        $compare = 0
        
        ;Read one line
        $ligne_transactmp_read = FileReadline($transactmp_read)
        ;If the file finish the loop finish
        If @error = -1 Then ExitLoop
        MsgBox(0, "Info", $ligne_transactmp_read)
        
        While 1
            
            $ligne_emaillog_read = FileReadline($emaillog_read)
            
            ;compare one line to all the lines of the other text file
            If $ligne_transactmp_read = $ligne_emaillog_read then
                $compare = 1
                MsgBox(0, "Error", "line ok")
            EndIf
            
            ;If the file finish the loop finish
            If @error = -1 Then ExitLoop
            MsgBox(0, "Info", $ligne_emaillog_read)
            
        WEnd        
            
        If $compare = 0 Then
            MsgBox(0, "Body Text", "new line detected")
            MsgBox(0, "Info", "opening emaillog.txt")
            
            ;Open emaillog.txt in write mode
            $emaillog_write = FileOpen(@ScriptDir & "\emaillog.txt", 1)
                
            ;Verify if file is open
            If $emaillog_write = -1 Then
                MsgBox(0, "Erreur", "unable to open the file")
                Exit
            EndIf
            
            ;write the line not found in emaillog.txt but found in transactmp.txt
            FileWriteLine($emaillog_write, $ligne_transactmp_read)
                
            FileClose($emaillog_write)
                
        EndIf
    WEnd
WEnd
    ;Closing txt files
    FileClose($transactmp_read)
    FileClose($emaillog_read)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

FileReadLine ( filehandle or "filename" [, line] )

Edited by Generator

Share this post


Link to post
Share on other sites

I use loop statements to do not specify de numbers of line in the text file

is it a good way ?

Share this post


Link to post
Share on other sites

Small Example

For $i=1 to _FileCountLines("file.txt")
    ConsoleWrite(FileReadLine("file.txt",$i) & @CRLF)
Next

Share this post


Link to post
Share on other sites

Another Example to Array

#include <File.au3>
#include <Array.au3>
Dim $oData[2]
For $i=1 to _FileCountLines("file.txt")
    $oData[$i]=FileReadLine("file.txt",$i)
    ReDim $oData[Ubound($oData)+1]
Next
_ArrayDisplay($oData)

Share this post


Link to post
Share on other sites

I change to loop process but it still not work

#include <File.au3>

While 1
        
    For $i=1 to _FileCountLines(@ScriptDir & "\emaillog.txt")
        $ligne_transactmp_read = FileReadLine(@ScriptDir & "\emaillog.txt",$i)
        $compare = 0
        
        MsgBox(0, "Info", $ligne_transactmp_read)
        
        For $q=1 to _FileCountLines(@ScriptDir & "\transactmp.txt")
            $ligne_emaillog_read = FileReadLine(@ScriptDir & "\transactmp.txt",$q)
            
            
            ;$ligne_emaillog_read = FileReadline($emaillog_read)
            
            ;compare one line to all the lines of the other text file
            If $ligne_transactmp_read = $ligne_emaillog_read then
                $compare = 1
                MsgBox(0, "Error", "line ok")
            EndIf
            
            ;If the file finish the loop finish
            ;If @error = -1 Then ExitLoop
            MsgBox(0, "Info", $ligne_emaillog_read)
            
        Next        
            
        If $compare = 0 Then
            MsgBox(0, "Body Text", "new line detected")
            MsgBox(0, "Info", "opening emaillog.txt")
            
            ;Open emaillog.txt in write mode
            $emaillog_write = FileOpen(@ScriptDir & "\emaillog.txt", 1)
                
            ;Verify if file is open
            If $emaillog_write = -1 Then
                MsgBox(0, "Erreur", "unable to open the file")
                Exit
            EndIf
            
            ;write the line not found in emaillog.txt but found in transactmp.txt
            FileWriteLine($emaillog_write, $ligne_transactmp_read)
                
            FileClose($emaillog_write)
                
        EndIf
    Next
WEnd
    ;Closing txt files
    FileClose($transactmp_read)
    FileClose($emaillog_read)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hello FreeSiker,

This is slow and lightly tested but may be helpful.

#include <File.au3>
#include <Array.au3>

Dim $sPathA = @DesktopDir & "\A.txt" 
Dim $sPathB = @DesktopDir & "\B.txt" 

Dim $aFileA
ConsoleWrite( "Reading in " & $sPathA & @CRLF)
_FileReadToArray( $sPathA, $aFileA )

Dim $aFileB
ConsoleWrite( "Reading in " & $sPathB & @CRLF)
_FileReadToArray( $sPathB, $aFileB )

ConsoleWrite( "Running comparison" & @CRLF)
For $i = $aFileA[0] to 1 Step -1
    For $x = $aFileB[0] to 1 Step -1        
        If $aFileB[$x] = $aFileA[$i] Then
            _ArrayDelete( $aFileA, $i )
            $aFileA[0] = UBound( $aFileA ) - 1
            _ArrayDelete( $aFileB, $x )
            $aFileB[0] = UBound( $aFileB ) - 1
            ExitLoop
        EndIf
    Next
Next

ConsoleWrite( "!!! " & $aFileA[0]  & " Line(s) unique to file A" & @CRLF)
$uniqueLinesA = _ArrayToString( $aFileA, @CRLF, 1 )
ConsoleWrite( $uniqueLinesA & @CRLF)

ConsoleWrite( "!!! " & $aFileB[0]  & " Line(s) unique to file B" & @CRLF)
$uniqueLinesB = _ArrayToString( $aFileB, @CRLF, 1 )
ConsoleWrite( $uniqueLinesB & @CRLF)

Some non-script, file comparison alternatives:

WinMerge

GNUDiff utilities

Hope this helps,

Zach...

Edited by zfisherdrums

Share this post


Link to post
Share on other sites

Wow that perfect !!!! Thank you so much

the code work very well now !!!

#include <File.au3>
#include <Array.au3>

Dim $sPathA = @DesktopDir & "\A.txt" 
Dim $sPathB = @DesktopDir & "\B.txt" 

While 1

Dim $aFileA
ConsoleWrite( "Reading in " & $sPathA & @CRLF)
_FileReadToArray( $sPathA, $aFileA )
;_ArrayDisplay($aFileA)

Dim $aFileB
ConsoleWrite( "Reading in " & $sPathB & @CRLF)
_FileReadToArray( $sPathB, $aFileB )
;_ArrayDisplay($aFileB)

ConsoleWrite( "Running comparison" & @CRLF)
For $i = $aFileA[0] to 1 Step -1
    For $x = $aFileB[0] to 1 Step -1        
        If $aFileB[$x] = $aFileA[$i] Then
            _ArrayDelete( $aFileA, $i )
            $aFileA[0] = UBound( $aFileA ) - 1
            _ArrayDelete( $aFileB, $x )
            $aFileB[0] = UBound( $aFileB ) - 1
            ExitLoop
        EndIf
    Next
Next

ConsoleWrite( "!!! " & $aFileA[0]  & " Line(s) unique to file A" & @CRLF)
$uniqueLinesA = _ArrayToString( $aFileA, @CRLF, 1 )
ConsoleWrite( $uniqueLinesA & @CRLF)

;Open txt in write mode
$emaillog_write = FileOpen($sPathB, 1)

;Verify if file is open
If $emaillog_write = -1 Then
    MsgBox(0, "Erreur", "unable to open the file")
    Exit
EndIf

If $uniqueLinesA = "" then
Else
FileWriteLine($emaillog_write, $uniqueLinesA)
FileClose($emaillog_write)
EndIf


ConsoleWrite( "!!! " & $aFileB[0]  & " Line(s) unique to file B" & @CRLF)
$uniqueLinesB = _ArrayToString( $aFileB, @CRLF, 1 )
ConsoleWrite( $uniqueLinesB & @CRLF)

sleep (10000)

WEndoÝ÷ Ú«¨µéÚ

Some non-script, file comparison alternatives:

WinMerge

GNUDiff utilities

Hope this helps,

Zach...

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hello FreeSiker,

This is slow and lightly tested but may be helpful.

....

I tryed similar way to compare to CSV files but it`s very slow (>30min). Is any way to compare two not indentical CSV files (~30`000 lines) faster? Edited by Vocis

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I tryed similar way to compare to CSV files but it`s very slow (>30min). Is any way to compare two not indentical CSV files (~30`000 lines) faster?

Mabe with a sql server like sqlite, import and export

but read this post

http://www.autoitscript.com/forum/index.php?showtopic=59755

Edited by FreeSiker

Share this post


Link to post
Share on other sites

Hi,

Yes, Scripting Dictionary works; speed fair? - maybe better ways too?

[Put your 2 files into arrys...]

; ArrayDupes2.au3
#include <array.au3>
;~ #include <string.au3>
Dim $ar_GUISearchar[10] = [9, 'apple1.txt', 'apple1.txt', 'apple12.txt', 'apple12.txt', 'apple16.txt', 'apple2.txt', 'apple2.txt', 'apple23.txt', 'apple23.txt']
Dim $ar_GUISearchar2[12] = [11, 'apple1.txt', 'apple1.txt', 'apple12.txt', 'apple124.txt', 'apple16.txt', 'apple2.txt', 'apple2.txt', 'apple23.txt', 'apple.txt', 'apple23.txt', 'apple23.txt']
;~ _ArrayDisplay($ar_GUISearchar, "Before")
;~ _ArrayDisplay($ar_GUISearchar2, "Before")
;========================================================================
$ArrayCompareExtra = _ArrayCompareExtra($ar_GUISearchar, $ar_GUISearchar2)
;========================================================================
If Not IsArray($ArrayCompareExtra) Then ConsoleWrite("$ArrayCompareExtra=" & $ArrayCompareExtra & @LF)
_ArrayDisplay($ArrayCompareExtra, "$ArrayCompareExtra")
Func _ArrayCompareExtra(ByRef $arrItems, ByRef $arrItems2)
    If @OSTYPE = "WIN32_WINDOWS"  Then Return 0
    Local $j = 0, $i = 0, $objDictionary = ObjCreate("Scripting.Dictionary"), $objDictionary2 = ObjCreate("Scripting.Dictionary"), $arrItems3[1]
    For $strItem In $arrItems
        If Not $objDictionary.Exists($strItem) And $i Then
            $objDictionary.Add($strItem, $strItem)
            $i += 1
        Else
            $i += 1
        EndIf
    Next
;~  $i = 0
    For $strItem In $arrItems2
        If Not $objDictionary2.Exists($strItem) And $j Then
            $objDictionary2.Add($strItem, $strItem)
            $j += 1
        Else
            $j += 1
        EndIf
    Next
;~  ReDim $arrItems3[$objDictionary.Count + $objDictionary2.Count ]
    ReDim $arrItems3[$i + $j ]
    ;================================
    $i = 1
    $j = 0
    For $strItem In $arrItems
        If Not $objDictionary2.Exists($strItem) And $j Then
            $arrItems3[$i] = $strItem & "|Extra $arrItems1"
            $i += 1
        Else
            $j += 1
        EndIf
    Next
    Local $j = 0
    For $strItem In $arrItems2
        If Not $objDictionary.Exists($strItem) And $j Then
            $arrItems3[$i] = $strItem & "|Extra $arrItems2"
            $i += 1
        Else
            $j += 1
        EndIf
    Next
    ReDim $arrItems3[$i ]
    $arrItems3[0 ] = UBound($arrItems3) - 1
    Return $arrItems3
EndFunc   ;==>_ArrayCompareExtra
Best, randall

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