Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Compare file date/time stamps


  • Please log in to reply
7 replies to this topic

#1 llewxam

llewxam

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 414 posts

Posted 05 February 2011 - 03:39 PM

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.


AutoIt         
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

My projects:

Spoiler

Find something helpful? BTC donations are appreciated!
16aheZKum8J32XPzk8dFj7AzX33An6zJ6d







#2 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 17,556 posts

Posted 05 February 2011 - 04:06 PM

Nice idea! Here is another way to achieve the Function you created. :)
Function:
AutoIt         
; #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, 25 May 2014 - 08:05 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()LockFile()Mapping CtrlIDsParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...
Updated: 26/09/2014


#3 llewxam

llewxam

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 414 posts

Posted 05 February 2011 - 07:16 PM

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

Thanks
Ian

My projects:

Spoiler

Find something helpful? BTC donations are appreciated!
16aheZKum8J32XPzk8dFj7AzX33An6zJ6d

#4 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,903 posts

Posted 09 February 2011 - 09:10 AM

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, 09 February 2011 - 09:29 AM.

       AutoIt Version : 3.3.12.0 / Beta 3.3.13.19 SciTE 3.4.1 OS:WIN7/WIN8.1 CPU:X64 OS:X64


#5 xavierh

xavierh

    Seeker

  • Active Members
  • 30 posts

Posted 25 August 2011 - 01:08 PM

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.



#6 xavierh

xavierh

    Seeker

  • Active Members
  • 30 posts

Posted 25 August 2011 - 01:23 PM

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 )

#7 ReFran

ReFran

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 554 posts

Posted 25 August 2011 - 07:58 PM

@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

#8 llewxam

llewxam

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 414 posts

Posted 26 August 2011 - 04:34 AM

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


AutoIt         
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, 26 August 2011 - 04:37 AM.

My projects:

Spoiler

Find something helpful? BTC donations are appreciated!
16aheZKum8J32XPzk8dFj7AzX33An6zJ6d




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users