Jump to content

Copy file and preserve all timestamps?


VAN0
 Share

Recommended Posts

FileCopy() preserves only lastmodifed timestamp. creationdate and lastaccessed timestamps are set at the moment of copy the file.

NTFS timestamp accuracy is in microseconds, but AutoIt can get it in milliseconds and set in seconds, making FileGetTime() and FileSetTime() not accurate enough.

How can I create an identical copy of a file with identical timestamps?

Thanks.

Test code:

#include <WinAPIFiles.au3>
Local $sNow = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC
$sFile = @TempDir & "\test" & $sNow & ".txt"
$sFile2 = $sFile & ".copy.txt"
FileWrite($sFile, $sNow)
Sleep(2111)
FileCopy($sFile, $sFile2)
FileSetTime($sFile2, FileGetTime($sFile, $FT_CREATED, 3), $FT_CREATED)
ConsoleWrite(@CRLF)
ConsoleWrite("orig FileGetTime: " & FileGetTime($sFile, $FT_CREATED, 3) & @CRLF)
ConsoleWrite("orig wmic       : " & getTime($sFile) & @CRLF)
ConsoleWrite("copy FileGetTime: " & FileGetTime($sFile2, $FT_CREATED, 3) & @CRLF)
ConsoleWrite("copy wmic       : " & getTime($sFile2) & @CRLF)
FileDelete($sFile)
FileDelete($sFile2)

Func getTime($sFile)
    Local $iPid = Run('wmic datafile where name="' & StringReplace($sFile, "\", "\\") & '" get creationdate', "", @SW_HIDE, 2)
    Local $sOut ;
    Do
        $sOut &= StdoutRead($iPid)
    Until @error
    Return StringRegExpReplace($sOut, "[^\d.-]", "")
EndFunc   ;==>getTime

 

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