Jump to content

Comparing 2 text files


stev379
 Share

Recommended Posts

This should be easy. I'm having a brain fart and can't seem to get it.

I want to read 2 text files and compare contents. Each line is the name of a printer. If a printer name is found on line 1 of the first file, but NOT found in any of the lines of the second file, then write it to a third text file.

My efforts keep returning same names over and over in the third file. This is just one version of many attempts. This should be easy. :">

$file1 = FileOpen("File1.txt", 0)
$file2 = FileOpen("File2.txt", 0)
$Compared = FileOpen("Compared.txt", 1)
$Compared_DIFF = FileOpen("Compared_DIFF.txt", 1)

If $file1 = -1 Then
    MsgBox(0, "Error", "Unable to open file1.")
    Exit
ElseIf $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file2.")
    Exit
EndIf

$i = 1
While 1
    $line1 = FileReadLine($file1, $i)
    If @error = -1 Then ExitLoop
      
    $n = 0
    While 1
        $Line2 = FileReadLine($file2, $n)
         If @error = -1 Then ExitLoop
             
        If $line2 <> $Line1 Then
    ;MsgBox(0, "Compare", $Line2 & " = " & $Line1)
        FileWriteLine($Compared_DIFF, $Line2)
        EndIf
        
        $n = $n + 1
    WEnd
    $i = $i + 1
Wend

FileClose($file1)
FileClose($file2)
FileClose($Compared)
FileClose($Compared_DIFF)
Edited by stev379
Link to comment
Share on other sites

quick... reply

try this

REPLACE from the $i = 1 to the last Wend

$i = 1
While 1
$line1 = FileReadLine($file1, $i)
If @error = -1 Then ExitLoop

$Line2 = FileReadLine($file2, $i)
If @error = -1 Then ExitLoop

If $line2 <> $Line1 Then
;MsgBox(0, "Compare", $Line2 & " = " & $Line1)
FileWriteLine($Compared_DIFF, $Line2)
EndIf

$i = $i + 1
Wend

8)

NEWHeader1.png

Link to comment
Share on other sites

I'm affraid the suggested solution does not work.

It's a bit more complicated than that but no so difficult.

I suggest you try the following attachments. Is it that you where looking for ?

Bye,

Peter

PS.

File 1 contains :

This is line 1

This is line 2

This is line 3

This is line 4

This is line 5

This is line 6

This is line 7

This is line 8

This is line 9

File 2 contains (in random order) :

This is line 1

This is line 2

This is line 3

This is line 5

This is line 7

This is line 8

This is line 9

This is line 10

There are 2 line in File 1 that are NOT in File 2 :

This is line 4

This is line 6

Compare_Two_Files.au3

file1.txt

file2.txt

Link to comment
Share on other sites

I changed it a little, so it uses arrays, and it works perfectly. Does it in less than a second too.

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

$FileName_1 = @scriptdir & "\File1.txt"
$FileName_2 = @scriptdir & "\File2.txt"
$FileName_Diff = @scriptdir & "\Compared_DIFF.txt"

Dim $File1Array[10], $File2Array[10]
Global $File1Offset = 0, $File2Offset = 0

; Create an empty file for writing the result to.
$File_3 = FileOpen($FileName_Diff, 2)
If $File_3 = -1 Then
    MsgBox(0, "Error", "Unable to create file : " & $FileName_Diff)
    Exit
EndIf
FileClose($File_3)


; Open the first file
$File_1 = FileOpen($FileName_1, 0)
If $File_1 = -1 Then
    MsgBox(0, "Error", "Unable to open file : " & $FileName_1)
    Exit
EndIf
$File_2 = FileOpen($FileName_2, 0)
If $File_2 = -1 Then
    MsgBox(0, "Error", "Unable to open file : " & $FileName_2)
    Exit
EndIf

_FileReadToArray ($FileName_1, $File1Array)
_FileReadToArray ($FileName_2, $File2Array)


For $i = 1 To UBound ($File1Array) - 1
    If _ArraySearch ($File2Array, $File1Array[$i]) = "" Then
        $DidWrite = Write_Diff_Log($FileName_Diff, $File1Array[$i])
        If $DidWrite = 0 Then
            MsgBox (0, "Error", "Could not write to file")
            Exit
        EndIf
    EndIf
Next

For $i = 1 To UBound ($File2Array) - 1
    If _ArraySearch ($File1Array, $File2Array[$i]) = "" Then
        Write_Diff_Log($FileName_Diff, $File2Array[$i])
        If $DidWrite = 0 Then
            MsgBox (0, "Error", "Could not write to file")
            Exit
        EndIf
    EndIf
Next


; This function adds a Message in a File
; It returns 0 on failure
; It returns 1 on success
Func Write_Diff_Log($LogFileName, $LogMessage)
    Local $File_3
    Local $WriteToFile_3

    $File_3 = FileOpen($LogFileName, 1)
    If $File_3 = -1 Then
        Return 0
    EndIf
    $WriteToFile_3 = FileWriteLine($File_3, $LogMessage)
    If $WriteToFile_3 = -1 Then
        Return 0
    EndIf
    FileClose($File_3)
    Return 1
EndFunc

Note: I added searching in the second file to find differences there too. Line 10 wasn't in the first file, so lines 4, 6, and 10 show up in the third. If you don't want that capability, just comment out the second For loop.

Link to comment
Share on other sites

It comes with Array.au3 in the Include directory of your Autoit installation.

But.. here it is pasted in case you don't have that....

Exactly from the Array.au3 file:

;===============================================================================
;
; Description:    Finds an entry within an one-dimensional array. (Similar to _ArrayBinarySearch() except the array does not need to be sorted.)
; Syntax:          _ArraySearch($avArray, $vWhat2Find, $iStart = 0, $iEnd = 0,$iCaseSense=0)
;
; Parameter(s):   $avArray         = The array to search
;                   $vWhat2Find     = What to search $avArray for
;                   $iStart (Optional) = Start array index for search, normally set to 0 or 1. If omitted it is set to 0
;                   $iEnd  (Optional)  = End array index for search. If omitted or set to 0 it is set to Ubound($AvArray)-
;                    $iCaseSense (Optional) = If set to 1 then search is case sensitive
; Requirement(s):   None
; Return Value(s):   On Success - Returns the position of an item in an array.
;                   On Failure - Returns an empty string "" if $vWhat2Find is not found
;                       @Error=1 $avArray is not an array
;                       @Error=2 $iStart is greater than UBound($AvArray)-1
;                       @Error=3 $iEnd is greater than UBound($AvArray)-1
;                       @Error=4 $iStart is greater than $iEnd
;                        @Error=5 $iCaseSense was invalid. (Must be 0 or 1)
; Author(s):        SolidSnake <MetalGearX91@Hotmail.com>
; Note(s):          This might be a bit slower than _ArrayBinarySearch() but is useful when the array's order can't be altered.
;===============================================================================
Func _ArraySearch($avArray, $vWhat2Find, $iStart = 0, $iEnd = 0, $iCaseSense = 0)
    Local $iCurrentPos, $iUBound
    If Not IsArray($avArray) Then
        SetError(1)
        Return ""
    EndIf
    $iUBound = UBound($avArray) - 1
    If $iEnd = 0 Then $iEnd = $iUBound
    If $iStart > $iUBound Then
        SetError(2)
        Return ""
    EndIf
    If $iEnd > $iUBound Then
        SetError(3)
        Return ""
    EndIf
    If $iStart > $iEnd Then
        SetError(4)
        Return ""
    EndIf
    If Not ($iCaseSense = 0 Or $iCaseSense = 1) Then
        SetError(5)
        Return ""
    EndIf
    For $iCurrentPos = $iStart To $iEnd
        Select
            Case $iCaseSense = 0
                If $avArray[$iCurrentPos] = $vWhat2Find Then
                    SetError(0)
                    Return $iCurrentPos
                EndIf
            Case $iCaseSense = 1
                If $avArray[$iCurrentPos] == $vWhat2Find Then
                    SetError(0)
                    Return $iCurrentPos
                EndIf
        EndSelect
        
    Next
    SetError(0)
    Return ""
EndFunc  ;==>_ArraySearch
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...