Jump to content

Recommended Posts

Posted

Hello Guys,

I wondering if you could give me some trigger/hint where to start:

I need to compare content of 2 files and a create a new file with the names (AA and BB) who are NOT in file2 --> file3

file1:

Koen

Evelyn

Emma

AA

BB

file2:

Koen 02 15

Evelyn

Emma 12

Jos

file3:

AA

BB

Many thanks ain advanced.

Best regards

Koen

Posted

Hi koenp,

use _FileReadToArray() to read both files to different arrays. Then you can use _ArraySearch() to look for the entries of array1 in array2.

Hope that gives you a general direction. :D

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Posted (edited)

Try something like this here:

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

Global $aFile1, $aFile2, $sFile1 = @ScriptDir & "File1.txt", $sFile2 = @ScriptDir & "File2.txt", $sFile3 = @ScriptDir & "File3.txt"
Gen_Files($sFile1, $sFile2)

_FileReadToArray($sFile1, $aFile1)
_FileReadToArray($sFile2, $aFile2)

_ArrayDiff2File($aFile1, $aFile2, $sFile3)

Func _ArrayDiff2File($a1, $a2, $sFileDiff) ;only for 1D arrays
    If Not IsArray($a1) Then Return SetError(1, 0, 0)
    If Not IsArray($a2) Then Return SetError(2, 0, 0)
    If UBound($a1, 2) > 1 Then Return SetError(3, 0, 0)
    If UBound($a2, 2) > 1 Then Return SetError(4, 0, 0)
    If UBound($a1) < 2 Then Return SetError(5, 0, 0)
    If UBound($a2) < 2 Then Return SetError(6, 0, 0)
    Local $i, $p
    _ArrayDelete($a1, 0)
    _ArrayDelete($a2, 0)
    $a1 = _ArrayUnique($a1)
    $a2 = _ArrayUnique($a2)
    For $i = 1 To $a1[0]
        $p = _ArraySearch2($a2, $a1[$i])
        If $p > 0 Then
            $a1[$i] = ""
        EndIf
    Next
    $a1[0] = ""
    $a1 = _ArrayUnique($a1)
    If $a1[1] = "" Then _ArrayDelete($a1, 1)
    _ArrayDelete($a1, 0)
    Local $hFile = FileOpen($sFileDiff, 2)
    If $hFile = -1 Then Return SetError(7, 0, 0)
    FileWrite($hFile, _ArrayToString($a1, @CRLF))
    FileClose($hFile)
;~     _ArrayDisplay($a1) ;for debugging only
    $a1 = 0
    $a2 = 0
    Return 1
EndFunc   ;==>_ArrayDiff2File

Func _ArraySearch2($a, $sElement, $bLike = True, $iStart = 0)
    Local $j
    For $j = $iStart To UBound($a) - 1
        If $bLike And StringInStr($a[$j], $sElement) And StringRight($a[$j], 1) <> @TAB Then
            Return $j
        Else
            If $a[$j] = $sElement Then Return $j
        EndIf
    Next
EndFunc   ;==>_ArraySearch2

Func Gen_Files($sFile1, $sFile2)
    Local $s1 = "Koen" & @CRLF & _
                            "Evelyn" & @CRLF & _
                            "Emma" & @CRLF & _
                            "AA" & @CRLF & _
                            "BB"
    Local $s2 = "Koen" & @TAB &  "02 15" & @CRLF & _
                            "Evelyn" & @TAB & @CRLF & _
                            "Emma 12" & @CRLF & _
                            "Jos"
    Local $hFile = FileOpen($sFile1, 2)
    FileWrite($hFile, $s1)
    FileClose($hFile)
    $hFile = FileOpen($sFile2, 2)
    FileWrite($hFile, $s2)
    FileClose($hFile)
EndFunc

Not fully tested!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

Thanks guys for all your help:

I am almost there

here the code that I untill now have:

There only thing that need to be correct is that the script only compares the first colum of the array, with other words:

It should be able to find/ignore "Koen" and "Emma" as well even with some digits after ( by the way the space between Koen and 02 is a @tab)

Many thanks for helping me ..

Best regards

Koen

file1:

Koen

Evelyn

Emma

AA

BB

file2:

Koen 02 15

Evelyn

Emma 12

Jos

#include <File.au3>
#include <Array.au3>
$file = FileOpen("C:UsersKoenDesktopFLnot_FL", 1)
Local $aArray_1 = 0, $aArray_2 = 0
_FileReadToArray("C:UsersKoenDesktopFLnewfile1.txt", $aArray_1) ; source
_FileReadToArray("C:UsersKoenDesktopFLnewfile2.txt", $aArray_2) ; look in
For $i = 1 To $aArray_1[0]
    For $j = 1 To $aArray_2[0]
        If StringStripWS($aArray_1[$i], 8) = StringStripWS($aArray_2[$j], 8) Then
            ContinueLoop 2
        EndIf
    Next
    ConsoleWrite('Not found: ' & $aArray_1[$i] & @CRLF)
; FileWriteLine($file, $aArray_1[$i] & @CRLF)
Next
Posted

I forgot something that could help:

Is it possible to read a text file line by line, stops reading when it find a blanc space and thne continue next line.

By this I could create a new file first inorder to remove the @tab 02 15 (example)?

br,

Koen

Posted

Hi Uez,

Thanks for this, Almost, the only thing is it should be the inverse, so the result should be the records from file1 who are not in file2.

For the rest your script looks perfect :-)

can you handle this?

thanks a lot,

Koen

Posted

koenp,

Why did you open up a Suffice to say I feel I wasted my time in your last post.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Just so you're aware for your future time here, creating duplicate topics is against the Forum Rules.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Hello

Can I as for one more request (variant the script you created):

Is it possible to ignore the records where there is something after the white @TAB:

So with the files below the result should be:

Evelyn

AA

BB

file1:

Koen

Evelyn

Emma

AA

BB

file2:

Koen 02 15

Evelyn

Emma 12

Jos

Is this is a hard thing to change?

Many thanks

Koen

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
  • Recently Browsing   0 members

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