Sign in to follow this  
Followers 0
msmou

Compare file text and size

7 posts in this topic

Two files are present.I want to compare the two files text by text(line by line) and verify if they are same and also compare their file size.Can anyone please suggest.

Thanks

Share this post


Link to post
Share on other sites



Does it have to be AutoIt or will another tool like Winmerge fit your needs too?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I have to do through Autoit Automation and then pass the result as success or fail

Share this post


Link to post
Share on other sites

Use function "FileGetSize" to retrieve the size of a file.

How big are the files you need to compare?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

20 kb file size .

FileGetSize will compare the file size.

But how to compare the file contents.

ex:I have a file ex: data.txt in PC location.I downloaded the same file data.txt from cloud.so i need to compare the file contents.If both file contents and size are same then pass msg as file downloaded successfully.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Something like this? Pass the two filenames to compare to the function.

; Compare FileSize and Content. Returns 1 if equal, 0 if not equal, -1 for error
Func _CompareFiles($sFile1, $sFile2)

    If FileGetSize($sFile1) <> FileGetSize($sFile2) Then Return 0
    $sFile1Content = FileRead($sFile1)
    If @error Then Return -1
    $sFile2Content = FileRead($sFile2)
    If @error Then Return -1
    If $sFile1Content <> $sFile2Content Then Return 0
    Return 1

EndFunc   ;==>_CompareFiles
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Here another way to compare line by line:

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

$aFileCompare = TextFileCompare("C:TempSilverlight0.log", "C:TempSilverlight1.log")
_ArrayDisplay($aFileCompare)

Func TextFileCompare($sFile1, $sFile2)
    If Not FileExists($sFile1) Or Not FileExists($sFile2) Then Return SetError(1, 0, 0) ;at least one file doesn't exist
    If Not FileGetSize($sFile1) Or Not FileGetSize($sFile1) Then Return SetError(2, 0, 0) ;at least one file is empty
    Local $aFile1, $aFile2, $i, $j = 1
    _FileReadToArray($sFile1, $aFile1)
    If @error Then Return SetError(3, @error, 0) ;file couldn't be read to the array
    _FileReadToArray($sFile2, $aFile2)
    If @error Then Return SetError(4, @error, 0) ;file couldn't be read to the array

    Local $aResult[Max($aFile1[0], $aFile2[0]) +2][3]
    Local $iMin = Min($aFile1[0], $aFile2[0])
    Local $bDiff = False
    For $i = 1 To $iMin
        If $aFile1[$i] <> $aFile2[$i] Then
            ConsoleWrite("Line " & $i & ": " & $aFile1[$i] & " <!> " &  $aFile2[$i] & @LF)
            $aResult[$j][0] = $i
            $aResult[$j][1] =  $aFile1[$i]
            $aResult[$j][2] =  $aFile2[$i]
            $bDiff = True
            $j += 1
        EndIf
    Next
    If $aFile1[0] > $aFile2[0]  Then
        $bDiff = True
        For $i = $aFile2[0] + 1 To $aFile1[0]
            ConsoleWrite("Line " & $i & ": " & $aFile1[$i] & " <!> " & @LF)
            $aResult[$j][0] = $i
            $aResult[$j][1] =  $aFile1[$i]
            $aResult[$j][2] =  ""
            $j += 1
        Next
    ElseIf $aFile1[0] < $aFile2[0] Then
        $bDiff = True
        For $i = $aFile1[0] + 1 To $aFile2[0]
            ConsoleWrite("Line " & $i & ":  " & " <!> " & $aFile2[$i]  & @LF)
            $aResult[$j][0] = $i
            $aResult[$j][1] =  ""
            $aResult[$j][2] =  $aFile2[$i]
            $j += 1
        Next
    EndIf
    If Not $bDiff Then
        ConsoleWrite("Files are equal!" & @LF)
        Return 0
    EndIf
    $aResult[0][0] = "Line"
    $aResult[0][1] =  $sFile1
    $aResult[0][2] =  $sFile2
    ReDim $aResult[$j][3]
    Return $aResult
EndFunc

Func Min($a, $b)
    If $a < $b Then Return $a
    Return $b
EndFunc

Func Max($a, $b)
    If $a > $b Then Return $a
    Return $b
EndFunc

Br,

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!
¯\_(ツ)_/¯

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