Sign in to follow this  
Followers 0

Compare file date/time stamps

8 posts in this topic

Posted

I had to figure this out for a recent tweak on my Sync Tool script, not sure if it will be useful for anyone else or any other purpose but thought I'd share it. Some forum searching found a few returns on _Date_Time_CompareFileTime but I found it to be a difficult thing to get set up, now the work is done for you! :)

Make sure you #Include Date.au3, WinAPI.au3, and Array.au3 in your script as well.

Func _CompareFileTimeEx($hSource, $hDestination, $iMethod)
;   Parameters ....:    $hSource -      Full path to the first file
;                       $hDestination - Full path to the second file
;                       $iMethod -      0   The date and time the file was created
;                                       1   The date and time the file was accessed
;                                       2   The date and time the file was modified
;   Return values .:                    -1  First file time is earlier than second file time
;                                       0   First file time is equal to second file time
;                                       1   First file time is later than second file time
;   Author ........:    Ian Maxwell (llewxam @ AutoIt forum)
    Local $hCurrent[2] = [$hSource, $hDestination], $tPointer[2] = ["", ""]
    For $iPointerCount = 0 To 1
        $hFile = _WinAPI_CreateFile($hCurrent[$iPointerCount], 2)
        $aTime = _Date_Time_GetFileTime($hFile)
        _WinAPI_CloseHandle($hFile)
        $aDate = _Date_Time_FileTimeToStr($aTime[$iMethod])
        $tFile = _Date_Time_EncodeFileTime(StringMid($aDate, 1, 2), StringMid($aDate, 4, 2), StringMid($aDate, 7, 4), StringMid($aDate, 12, 2), StringMid($aDate, 15, 2), StringMid($aDate, 18, 2))
        $aDOS = _Date_Time_FileTimeToDOSDateTime(DllStructGetPtr($tFile))
        $tFileTime = _Date_Time_DOSDateTimeToFileTime("0x" & Hex($aDOS[0], 4), "0x" & Hex($aDOS[1], 4))
        $tPointer[$iPointerCount] = DllStructGetPtr($tFileTime)
    Next
    Return _Date_Time_CompareFileTime($tPointer[0], $tPointer[1])
EndFunc   ;==>_CompareFileTimeEx

Enjoy

Ian

Share this post


Link to post
Share on other sites



Posted (edited)

Nice idea! Here is another way to achieve the Function you created. :)

Function:

; #FUNCTION# ====================================================================================================================
; Name ..........: _FileCompare
; Description ...: Checks the date between two files.
; Syntax ........: _FileCompare($sSource, $sDestination[, $iMethod = 0])
; Parameters ....: $sSource             - A valid file path.
;                  $sDestination        - A valid file path.
;                  $iMethod             - [optional] The timestamp to check. See FileGetTime()'s options. Default is $FT_MODIFIED.
; Return values .: Success - -1 (File 1 is earlier than File 2.)
;                  0 (File 1 is equal to File 2.)
;                  1 (File 1 is older than File 2.)
; Author ........: guinness
; Modified ......:
; Remarks .......: Date.au3 should be included.
; Example .......: Yes
; ===============================================================================================================================
Func _FileCompare($sSource, $sDestination, $iMethod = 0)
    Local $iDate1 = StringRegExpReplace(FileGetTime($sSource, $iMethod, 1), '(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})', '\1/\2/\3 \4:\5:\6')
    Local $iDate2 = StringRegExpReplace(FileGetTime($sDestination, $iMethod, 1), '(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})', '\1/\2/\3 \4:\5:\6')
    Local $iDateDiff = _DateDiff('s', $iDate2, $iDate1)
    If $iDateDiff > 0 Then Return -1
    If $iDateDiff = 0 Then Return 0
    Return 1
EndFunc   ;==>_FileCompare
Example use of Function:

#include <Date.au3> ; Required for _DateDiff()
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $aArray[3] = [2]
    For $i = 1 To $aArray[0]
        $aArray[$i] = FileOpenDialog('_FileCompare()', @ScriptDir, 'All (*.*)')
        If @error Then Return False
    Next
    MsgBox($MB_SYSTEMMDOAL, '', _FileCompare($aArray[1], $aArray[2]))
    Return True
EndFunc   ;==>Example
Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Nice twist, and I have to get in to StringRegExp(Replace), looks so powerful but is intimidating!

Thanks

Ian

Share this post


Link to post
Share on other sites

Posted (edited)

Another way Posted Image

#include <Date.au3>

; determine how many days a file was created.
MsgBox ( 0, "", "Number of days since creation : " & _DateDiff ( 'd', _ConvertTimeFormat ( FileGetTime ( "c:\boot.ini", 1, 1 ) ), _NowCalc ( ) ) )

; determine the difference in days of creation between two files.
MsgBox ( 0, "", "Number of days between this 2 files : " & _DateDiff ( 'd', _ConvertTimeFormat ( FileGetTime ( @WindowsDir & "\explorer.exe", 1, 1 ) ), _ConvertTimeFormat ( FileGetTime ( "c:\boot.ini", 1, 1 ) ) ) )


Func _ConvertTimeFormat ( $_FileTime ) ; convert 20100716213616 string time format to this time format YYYY/MM/DD HH:MM:SS
    Return StringMid ( $_FileTime, 1 , 4 ) & '/' & StringMid ( $_FileTime, 5 , 2 ) & '/' & StringMid ( $_FileTime, 7 , 2 ) & _
    ' ' & StringMid ( $_FileTime, 9 , 2 ) & ':' & StringMid ( $_FileTime, 11 , 2 ) & ':' & StringMid ( $_FileTime, 13 , 2 )
EndFunc ;==> _ConvertTimeFormat ( )

A Positive Result : first file is older than second

a Negative Result : first file is newer than second

and a Null Result : first file was created same date than second.

Edited by wakillon

Share this post


Link to post
Share on other sites

Posted

Another way Posted Image

#include <Date.au3>

; determine how many days a file was created.
MsgBox ( 0, "", "Number of days since creation : " & _DateDiff ( 'd', _ConvertTimeFormat ( FileGetTime ( "c:\boot.ini", 1, 1 ) ), _NowCalc ( ) ) )

; determine the difference in days of creation between two files.
MsgBox ( 0, "", "Number of days between this 2 files : " & _DateDiff ( 'd', _ConvertTimeFormat ( FileGetTime ( @WindowsDir & "\explorer.exe", 1, 1 ) ), _ConvertTimeFormat ( FileGetTime ( "c:\boot.ini", 1, 1 ) ) ) )


Func _ConvertTimeFormat ( $_FileTime ) ; convert 20100716213616 string time format to this time format YYYY/MM/DD HH:MM:SS
    Return StringMid ( $_FileTime, 1 , 4 ) & '/' & StringMid ( $_FileTime, 5 , 2 ) & '/' & StringMid ( $_FileTime, 7 , 2 ) & _
    ' ' & StringMid ( $_FileTime, 9 , 2 ) & ':' & StringMid ( $_FileTime, 11 , 2 ) & ':' & StringMid ( $_FileTime, 13 , 2 )
EndFunc ;==> _ConvertTimeFormat ( )

A Positive Result : first file is older than second

a Negative Result : first file is newer than second

and a Null Result : first file was created same date than second.

Share this post


Link to post
Share on other sites

Posted

I can think of a simple way to compare 2 file dates. Just create a string of format YYMMDDHHMMSS from the file date array of each file, then compare the values numerically

example:

$arTime1 = FileGetTime ( $SourceFoundFile )

$SrcFileTime = $arTime1 [ 0 ] & $arTime1 [ 1 ] & $arTime1 [ 2 ] & $arTime1 [ 3 ] & $arTime1 [ 4 ] & $arTime1 [ 5 ]

$arTime2 = FileGetTime ( $TargetFile )

$TgtFileTime = $arTime2 [ 0 ] & $arTime2 [ 1 ] & $arTime2 [ 2 ] & $arTime2 [ 3 ] & $arTime2 [ 4 ] & $arTime2 [ 5 ]

if $SrcFileTime > $TgtFileTime Then FileCopy ( $SourceFile, $TargetFile )

Share this post


Link to post
Share on other sites

Posted

@xavierh

Only as remarK: FileGetTime ( "filename" [, option [, format]] )

where format 1 = return a string YYYYMMDDHHMMSS

That I change normally to the more practical format: YYYYMMDD.HHMMSS

br, Reinhard

Share this post


Link to post
Share on other sites

Posted (edited)

I have re-written this recently, it turns out that I made it MUCH harder than it should have been. Melba23 pointed out FileGetTime, and I am still not quite sure how I missed it while looking through the help file :mellow:

Switching isn't really "recommended" as the original worked well, but this one is definitely a lot simpler! Be warned though that the syntax has changed slightly due to the functions in Date.au3 having different values used for Create, Access, and Modify.

Enjoy

Ian

Func _CompareFileTimeEx($hSource, $hDestination, $iMethod)
    ;Parameters ....:       $hSource -      Full path to the first file
    ;                       $hDestination - Full path to the second file
    ;                       $iMethod -      0   The date and time the file was modified
    ;                                       1   The date and time the file was created
    ;                                       2   The date and time the file was accessed
    ;Return values .:                       -1  The Source file time is earlier than the Destination file time
    ;                                       0   The Source file time is equal to the Destination file time
    ;                                       1   The Source file time is later than the Destination file time
    ;Author ........:       Ian Maxwell (llewxam @ AutoIt forum)
    $aSource = FileGetTime($hSource, $iMethod, 0)
    $aDestination = FileGetTime($hDestination, $iMethod, 0)
    For $a = 0 To 5
        If $aSource[$a] <> $aDestination[$a] Then
            If $aSource[$a] < $aDestination[$a] Then
                Return -1
            Else
                Return 1
            EndIf
        EndIf
    Next
    Return 0
EndFunc   ;==>_CompareFileTimeEx

edit: Oh, one nice improvement is that no other Includes are needed any more, so another plus for simplicity....

Edited by llewxam

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

  • Recently Browsing   0 members

    No registered users viewing this page.