Jump to content

Comparing two text files


slayerz
 Share

Recommended Posts

Hi everyone...

I had tried to compare between two text files, first by comparing between FileGetSize($fileA) and FileGetSize($fileB) whether they're equal.

If not, then I proceed to _FileReadToArray() function and compare line by line.

The problem is, _FileReadToArray() does not compare any extra blank lines.

Let say, content of file A is:

1 2
3 4
5 6
<--- this is the extra line

and, content of file B is (without extra line):

1 2
3 4
5 6

My script will detect it as the same content.

Using FileRead also give the same result.

Can anyone point me to some example like this?

Thanks

Edited by slayerz

AUTOIT[sup] I'm lovin' it![/sup]

Link to comment
Share on other sites

Thanks for replying...well I guess I have to user external application to make the compare jobs.

You mean you don't like the idea of adding a couple of lines to your script so you'll do something else? Why are you writing code at all? Or do you mean that if I say that then someone will show me how to do it so then I won't have to bother with all that nasty thinking stuff?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 5 weeks later...

You mean you don't like the idea of adding a couple of lines to your script so you'll do something else? Why are you writing code at all? Or do you mean that if I say that then someone will show me how to do it so then I won't have to bother with all that nasty thinking stuff?

No, I already tested various ways, and I think it's the best to integrate 3rd party software to make the compare and give the output to my program because it's more accurate, like WinMerge.

AUTOIT[sup] I'm lovin' it![/sup]

Link to comment
Share on other sites

I used a modified version of Wards MD5 functions from here to do it. The files must be EXACT to match.

Global $_MD5Opcode = '0xC85800005356576A006A006A008D45A850E8280000006A00FF750CFF75'
$_MD5Opcode &= '088D45A850E8440000006A006A008D45A850FF7510E8710700005F5E5BC9C210005'
$_MD5Opcode &= '589E58B4D0831C0894114894110C70101234567C7410489ABCDEFC74108FEDCBA98C'
$_MD5Opcode &= '7410C765432105DC21000C80C0000538B5D088B4310C1E80383E03F8945F88B4510C'
$_MD5Opcode &= '1E0030143103943107303FF43148B4510C1E81D0143146A40582B45F88945F4394510'
$_MD5Opcode &= '724550FF750C8B45F88D44031850E8A00700008D43185053E84E0000008B45F48945FC'
$_MD5Opcode &= '8B45FC83C03F39451076138B450C0345FC5053E8300000008345FC40EBE28365F800E'
$_MD5Opcode &= 'B048365FC008B45102B45FC508B450C0345FC508B45F88D44031850E84D0700005BC9C'
$_MD5Opcode &= '21000C84000005356576A40FF750C8D45C050E8330700008B45088B088B50048B70088B'
$_MD5Opcode &= '780C89D021F089D3F7D321FB09D801C1034DC081C178A46AD7C1C10701D189C821D089CB'
$_MD5Opcode &= 'F7D321F309D801C7037DC481C756B7C7E8C1C70C01CF89F821C889FBF7D321D309D801C60'
$_MD5Opcode &= '375C881C6DB702024C1C61101FE89F021F889F3F7D321CB09D801C20355CC81C2EECEBDC1'
$_MD5Opcode &= 'C1C21601F289D021F089D3F7D321FB09D801C1034DD081C1AF0F7CF5C1C10701D189C821D0'
$_MD5Opcode &= '89CBF7D321F309D801C7037DD481C72AC68747C1C70C01CF89F821C889FBF7D321D309D801'
$_MD5Opcode &= 'C60375D881C6134630A8C1C61101FE89F021F889F3F7D321CB09D801C20355DC81C2019546FD'
$_MD5Opcode &= 'C1C21601F289D021F089D3F7D321FB09D801C1034DE081C1D8988069C1C10701D189C821D089'
$_MD5Opcode &= 'CBF7D321F309D801C7037DE481C7AFF7448BC1C70C01CF89F821C889FBF7D321D309D801C603'
$_MD5Opcode &= '75E881C6B15BFFFFC1C61101FE89F021F889F3F7D321CB09D801C20355EC81C2BED75C89C1C21'
$_MD5Opcode &= '601F289D021F089D3F7D321FB09D801C1034DF081C12211906BC1C10701D189C821D089CBF7D3'
$_MD5Opcode &= '21F309D801C7037DF481C7937198FDC1C70C01CF89F821C889FBF7D321D309D801C60375F881C'
$_MD5Opcode &= '68E4379A6C1C61101FE89F021F889F3F7D321CB09D801C20355FC81C22108B449C1C21601F289D'
$_MD5Opcode &= '021F889FBF7D321F309D801C1034DC481C162251EF6C1C10501D189C821F089F3F7D321D309D'
$_MD5Opcode &= '801C7037DD881C740B340C0C1C70901CF89F821D089D3F7D321CB09D801C60375EC81C6515A5'
$_MD5Opcode &= 'E26C1C60E01FE89F021C889CBF7D321FB09D801C20355C081C2AAC7B6E9C1C21401F289D021F'
$_MD5Opcode &= '889FBF7D321F309D801C1034DD481C15D102FD6C1C10501D189C821F089F3F7D321D309D801C'
$_MD5Opcode &= '7037DE881C753144402C1C70901CF89F821D089D3F7D321CB09D801C60375FC81C681E6A1D8C1'
$_MD5Opcode &= 'C60E01FE89F021C889CBF7D321FB09D801C20355D081C2C8FBD3E7C1C21401F289D021F889FBF7'
$_MD5Opcode &= 'D321F309D801C1034DE481C1E6CDE121C1C10501D189C821F089F3F7D321D309D801C7037D'
$_MD5Opcode &= 'F881C7D60737C3C1C70901CF89F821D089D3F7D321CB09D801C60375CC81C6870DD5F4C1C60E01'
$_MD5Opcode &= 'FE89F021C889CBF7D321FB09D801C20355E081C2ED145A45C1C21401F289D021F889FBF7D321F30'
$_MD5Opcode &= '9D801C1034DF481C105E9E3A9C1C10501D189C821F089F3F7D321D309D801C7037DC881C7F8A3EFF'
$_MD5Opcode &= 'CC1C70901CF89F821D089D3F7D321CB09D801C60375DC81C6D9026F67C1C60E01FE89F021C889CBF7'
$_MD5Opcode &= 'D321FB09D801C20355F081C28A4C2A8DC1C21401F289D031F031F801C1034DD481C14239FAFFC1C'
$_MD5Opcode &= '10401D189C831D031F001C7037DE081C781F67187C1C70B01CF89F831C831D001C60375EC81C62261'
$_MD5Opcode &= '9D6DC1C61001FE89F031F831C801C20355F881C20C38E5FDC1C21701F289D031F031F801C1034DC48'
$_MD5Opcode &= '1C144EABEA4C1C10401D189C831D031F001C7037DD081C7A9CFDE4BC1C70B01CF89F831C831D001C60'
$_MD5Opcode &= '375DC81C6604BBBF6C1C61001FE89F031F831C801C20355E881C270BCBFBEC1C21701F289D031F031F8'
$_MD5Opcode &= '01C1034DF481C1C67E9B28C1C10401D189C831D031F001C7037DC081C7FA27A1EAC1C70B01CF89F831C8'
$_MD5Opcode &= '31D001C60375CC81C68530EFD4C1C61001FE89F031F831C801C20355D881C2051D8804C1C21701F289D03'
$_MD5Opcode &= '1F031F801C1034DE481C139D0D4D9C1C10401D189C831D031F001C7037DF081C7E599DBE6C1C70B01CF89F'
$_MD5Opcode &= '831C831D001C60375FC81C6F87CA21FC1C61001FE89F031F831C801C20355C881C26556ACC4C1C21701F289'
$_MD5Opcode &= 'F8F7D009D031F001C1034DC081C1442229F4C1C10601D189F0F7D009C831D001C7037DDC81C797FF2A43C1C'
$_MD5Opcode &= '70A01CF89D0F7D009F831C801C60375F881C6A72394ABC1C60F01FE89C8F7D009F031F801C20355D481C'
$_MD5Opcode &= '239A093FCC1C21501F289F8F7D009D031F001C1034DF081C1C3595B65C1C10601D189F0F7D009C831D001'
$_MD5Opcode &= 'C7037DCC81C792CC0C8FC1C70A01CF89D0F7D009F831C801C60375E881C67DF4EFFFC1C60F01FE89C8F7D0'
$_MD5Opcode &= '09F031F801C20355C481C2D15D8485C1C21501F289F8F7D009D031F001C1034DE081C14F7EA86FC1C1060'
$_MD5Opcode &= '1D189F0F7D009C831D001C7037DFC81C7E0E62CFEC1C70A01CF89D0F7D009F831C801C60375D881C61443'
$_MD5Opcode &= '01A3C1C60F01FE89C8F7D009F031F801C20355F481C2A111084EC1C21501F289F8F7D009D031F001C1034D'
$_MD5Opcode &= 'D081C1827E53F7C1C10601D189F0F7D009C831D001C7037DEC81C735F23ABDC1C70A01CF89D0F7D009F831'
$_MD5Opcode &= 'C801C60375C881C6BBD2D72AC1C60F01FE89C8F7D009F031F801C20355E481C291D386EBC1C21501F28B45'
$_MD5Opcode &= '08010801500401700801780C5F5E5BC9C20800C814000053E8400000008000000000000000000000000000'
$_MD5Opcode &= '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
$_MD5Opcode &= '00000000000008F45EC8B5D0C6A088D4310508D'
$_MD5Opcode &= '45F850E8510000008B4310C1E80383E03F8945F483F838730B6A38582B45F48945F0EB096A78582B45F489'
$_MD5Opcode &= '45F0FF75F0FF75ECFF750CE831F8FFFF6A088D45F850FF750CE823F8FFFF6A1053FF7508E8050000005BC9'
$_MD5Opcode &= 'C210005589E55156578B7D088B750C8B4D10FCF3A45F5E595DC20C00'
Global $BufferSize = 0x20000

Func _File_Compare2($sFile_1, $sFile_2)
    If FileExists($sFile_1) And FileExists($sFile_2) Then ;; Make sure they are valid file paths
        Local $Hash1, $Hash2
        Local $FileHandle = FileOpen($sFile_1, 16)
        Local $MD5CTX = _MD5Init()

        For $i = 1 To Ceiling(FileGetSize($sFile_1) / $BufferSize)
            _MD5Input($MD5CTX, FileRead($FileHandle, $BufferSize))
        Next

        $Hash1 = _MD5Result($MD5CTX)
        $FileHandle = FileOpen($sFile_2, 16)
        $MD5CTX = _MD5Init()

        For $i = 1 To Ceiling(FileGetSize($sFile_2) / $BufferSize)
            _MD5Input($MD5CTX, FileRead($FileHandle, $BufferSize))
        Next

        $Hash2 = _MD5Result($MD5CTX)
        FileClose($FileHandle)
        Return $Hash1 = $Hash2
    Else ;; one of the files does not exist
        SetError(1)
        Return -1
    EndIf
EndFunc    ;<===> _File_Compare2()

Func _MD5Init()
    If Not IsDeclared("_MD5CodeBuffer") Or Not IsDllStruct($_MD5CodeBuffer) Then
        Global $_MD5CodeBuffer = DllStructCreate("byte[" & BinaryLen($_MD5Opcode) & "]")
        DllStructSetData($_MD5CodeBuffer, 1, $_MD5Opcode)
    EndIf
    ;Local $OpcodeStart = (StringInStr($_MD5Opcode, "5589E58B4D08") - 1) / 2 - 1
    Local $OpcodeStart = 62
    Local $MD5CTX = DllStructCreate("dword[22]")
    DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($_MD5CodeBuffer) + $OpcodeStart, _
            "ptr", DllStructGetPtr($MD5CTX), _
            "int", 0, _
            "int", 0, _
            "int", 0)
    $CodeBuffer = 0
    Return $MD5CTX
EndFunc    ;<===> _MD5Init()

Func _MD5Input(ByRef $MD5CTX, $Data)
    If Not IsDeclared("_MD5CodeBuffer") Or Not IsDllStruct($_MD5CodeBuffer) Then Return
    ;Local $OpcodeStart = (StringInStr($_MD5Opcode, "C80C0000538B5D08") - 1) / 2 - 1
    Local $OpcodeStart = 107
    Local $Input = DllStructCreate("byte[" & BinaryLen($Data) & "]")
    DllStructSetData($Input, 1, $Data)
    DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($_MD5CodeBuffer) + $OpcodeStart, _
            "ptr", DllStructGetPtr($MD5CTX), _
            "ptr", DllStructGetPtr($Input), _
            "int", BinaryLen($Data), _
            "int", 0)
    $Input = 0
EndFunc    ;<===> _MD5Input()

Func _MD5Result(ByRef $MD5CTX)
    If Not IsDeclared("_MD5CodeBuffer") Or Not IsDllStruct($_MD5CodeBuffer) Then Return Binary(0)
    ;Local $OpcodeStart = (StringInStr($_MD5Opcode, "C814000053") - 1) / 2 - 1
    Local $OpcodeStart = 1960
    Local $Digest = DllStructCreate("byte[16]")
    DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($_MD5CodeBuffer) + $OpcodeStart, _
            "ptr", DllStructGetPtr($Digest), _
            "ptr", DllStructGetPtr($MD5CTX), _
            "int", 0, _
            "int", 0)
    Local $Ret = DllStructGetData($Digest, 1)
    $CodeBuffer = 0
    $Digest = 0
    $MD5CTX = 0
    $_MD5CodeBuffer = 0
    Return $Ret
EndFunc    ;<===> _MD5Result()

To call it just use something like

$sFile_1 = "C:\Some\Path\file1.au3"
$sFile_2 = "C:\Some\Path\file2.au3"
If _File_Compare2($sFile_1, $sFile_2) Then
    MsgBox(0, "Result", "Files match.")
Else
    MsgBox(0, "Result", "Files do not match.")
EndIf

EDIT: Actually just scrap that. This works and it's easier.

MsgBox(0, "Read Result", FileRead(@DesktopDir & "\test1.txt") = FileRead(@DesktopDir & "\test2.txt"))
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...