Jump to content

Compare file date/time stamps


llewxam
 Share

Recommended Posts

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

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

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

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_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()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

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

Thanks

Ian

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

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

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

  • 6 months later...

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.

Link to comment
Share on other sites

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 )

Link to comment
Share on other sites

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

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

  • 4 years later...

I'm glad it was of use, and twice as glad that it still works, it is pretty old! :)

 

Ian

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

@llewxam i am only few days older in Autoit  World, so any script is new for me :)

I am after something similar

what i am trying to achieve is

modify the code so that it checks i.html's date in G drive (if the files are older) and it over writes with new file from the source.

What the code does is maps the system by getting IP address from $address and copies file into the drive.

func latestIndexHTML($address, $name)
   Local $result
   Local $hFileIndexHTML
   DriveMapDel ( "G:" )
   ConsoleWrite('Trying: ' & $address & "/" & $name & @CRLF);
   $result = DriveMapAdd ( "G:", "\\" & $address & "\C$", 0, $name & "\admin","adminpswd")
   if $result = 1 Then
      FileCopy( "C:\a\i.html", "G:\Program Files\L\LC\", 1);
      ConsoleWrite('Copying I.html file: ' & $address & @CRLF);
      $hFileIndexHTML = FileOpen("C:\a\ihtml.txt", 1)
      FileWrite($hFileIndexHTML, $address & @CRLF)
      FileClose($hFileIndexHTML)
   EndIf
EndFunc

any help will be really great

Link to comment
Share on other sites

What you would do is use one of the above methods prior to FileCopy to compare the 2 file's dates (by last modified date), and if your source file is newer than the destination file, go ahead with the FileCopy.

 

I'm not seeing what value $hFileIndexHTML should have, but you get the idea.

 

Ian

 

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
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...