Jump to content

File not returning a valid time/date


VADemon
 Share

Recommended Posts

I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.

In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.

Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?

_WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()

Func _SetFileTimes($sFilePath)
    Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"]
    Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    
    Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only
    ; may NOT return a valid date for some reason! TODO
    Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle)
    _WinAPI_CloseHandle($fHandle)
    
    ; This will return an empty array if theres no valid date
    $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified
    if @error <> 10 then
        Local $year = $fModTime[2]
        Local $month = $fModTime[0]
        Local $day = $fModTime[1]
        Local $hour = $fModTime[3]
        Local $min = $fModTime[4]
        Local $sec = $fModTime[5]
        Local $ms = $fModTime[6]
        Local $weekday = $fModTime[7]
        
        Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec)
        Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day)
        
        $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified
        Local $year = $fModTime[2]
        Local $month = $fModTime[0]
        Local $day = $fModTime[1]
        Local $hour = $fModTime[3]
        Local $min = $fModTime[4]
        Local $sec = $fModTime[5]
        Local $ms = $fModTime[6]
        Local $weekday = $fModTime[7]
        
        ; GetUnixTime accounts for Local time, hence feed it local time
        Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec)
    else
        Global $prettyTimestamp = "N/A"
        Global $uploadDate = ""
        Global $unixTimestamp = "N/A"
    endif
    
    
endfunc

 

_GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)

The file reports these dates in Explorer, it's on local NTFS drive:

Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02

Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23

Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23

Link to comment
Share on other sites

Try FileGetTime.

Other than that, the file is in use by some program that locks it or something, or is in read mode.

Dont see other options. Maybe a bug.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

I don't know it help help for you or not:

#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>
#include <Date.au3>
#include <FileConstants.au3>

Global $iFileToWork = @ScriptFullPath

FileSetTime($iFileToWork, @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC, $FT_CREATED)
FileSetTime($iFileToWork, @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC, $FT_MODIFIED)
FileSetTime($iFileToWork, @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC, $FT_ACCESSED)

_FileGetTime($iFileToWork)


Func _FileGetTime($sFilePath)

    Local $Created_ArrayFileTime = FileGetTime($sFilePath, $FT_CREATED) ;  $FT_CREATED (1) = Created
    Local $Created_iYear = $Created_ArrayFileTime[0] ; = year (four digits)
    Local $Created_iMonth = $Created_ArrayFileTime[1] ; = month (range 01 - 12)
    Local $Created_iDay = $Created_ArrayFileTime[2] ; = day (range 01 - 31)
    Local $Created_iHour = $Created_ArrayFileTime[3] ; = hour (range 00 - 23)
    Local $Created_iMin = $Created_ArrayFileTime[4] ; = min (range 00 - 59)
    Local $Created_iSec = $Created_ArrayFileTime[5] ; = sec (range 00 - 59)
    Local $Created_iDayOfWeek = _DateToDayOfWeek($Created_iYear, $Created_iMonth, $Created_iDay)
    Local $Created_iDayName = _DateDayOfWeek($Created_iDayOfWeek)
    Local $Created_iMonthName = _DateToMonth($Created_iMonth)
    Local $Created_iShortTime = $Created_iYear & $Created_iMonth & $Created_iDay & $Created_iHour & $Created_iMin & $Created_iSec
    Local $Created_iLongTime = $Created_iYear & "/" & $Created_iMonth & "/" & $Created_iDay & " - " & $Created_iHour & ":" & $Created_iMin & ":" & $Created_iSec
    Local $Created_iLongTimeName = $Created_iDayName & "," & $Created_iMonthName & " " & $Created_iDay & "," & $Created_iYear
    ConsoleWrite("! File   : " & $sFilePath & @CRLF)
    ConsoleWrite("-DATE Created  : " & $Created_iLongTimeName & @CRLF)
    ConsoleWrite(">DATE Created S: " & $Created_iLongTime & @CRLF)
    ConsoleWrite("+DATE Created L: " & $Created_iShortTime & @CRLF)
    ConsoleWrite("-Year     : " & $Created_iYear & @CRLF)
    ConsoleWrite("-Mont     : " & $Created_iMonth & @CRLF)
    ConsoleWrite("-Day      : " & $Created_iDay & @CRLF)
    ConsoleWrite("-Hour     : " & $Created_iHour & @CRLF)
    ConsoleWrite("-Min      : " & $Created_iMin & @CRLF)
    ConsoleWrite("-Sec      : " & $Created_iSec & @CRLF)
    ConsoleWrite("-DayOfWeek: " & $Created_iDayOfWeek & @CRLF)
    ConsoleWrite("-DayName  : " & $Created_iDayName & @CRLF)
    ConsoleWrite("-MonthName: " & $Created_iMonthName & @CRLF)

    Local $Modified_ArrayFileTime = FileGetTime($sFilePath, $FT_MODIFIED) ;$FT_MODIFIED (0) = Last modified
    Local $Modified_iYear = $Modified_ArrayFileTime[0] ; = year (four digits)
    Local $Modified_iMonth = $Modified_ArrayFileTime[1] ; = month (range 01 - 12)
    Local $Modified_iDay = $Modified_ArrayFileTime[2] ; = day (range 01 - 31)
    Local $Modified_iHour = $Modified_ArrayFileTime[3] ; = hour (range 00 - 23)
    Local $Modified_iMin = $Modified_ArrayFileTime[4] ; = min (range 00 - 59)
    Local $Modified_iSec = $Modified_ArrayFileTime[5] ; = sec (range 00 - 59)
    Local $Modified_iDayOfWeek = _DateToDayOfWeek($Modified_iYear, $Modified_iMonth, $Modified_iDay)
    Local $Modified_iDayName = _DateDayOfWeek($Modified_iDayOfWeek)
    Local $Modified_iMonthName = _DateToMonth($Modified_iMonth)
    Local $Modified_iShortTime = $Modified_iYear & $Modified_iMonth & $Modified_iDay & $Modified_iHour & $Modified_iMin & $Modified_iSec
    Local $Modified_iLongTime = $Modified_iYear & "/" & $Modified_iMonth & "/" & $Modified_iDay & " - " & $Modified_iHour & ":" & $Modified_iMin & ":" & $Modified_iSec
    Local $Modified_iLongTimeName = $Modified_iDayName & "," & $Modified_iMonthName & " " & $Modified_iDay & "," & $Modified_iYear
    ConsoleWrite("! File   : " & $sFilePath & @CRLF)
    ConsoleWrite("-DATE Modified  : " & $Modified_iLongTimeName & @CRLF)
    ConsoleWrite(">DATE Modified S: " & $Modified_iLongTime & @CRLF)
    ConsoleWrite("+DATE Modified L: " & $Modified_iShortTime & @CRLF)
    ConsoleWrite("-Year     : " & $Modified_iYear & @CRLF)
    ConsoleWrite("-Mont     : " & $Modified_iMonth & @CRLF)
    ConsoleWrite("-Day      : " & $Modified_iDay & @CRLF)
    ConsoleWrite("-Hour     : " & $Modified_iHour & @CRLF)
    ConsoleWrite("-Min      : " & $Modified_iMin & @CRLF)
    ConsoleWrite("-Sec      : " & $Modified_iSec & @CRLF)
    ConsoleWrite("-DayOfWeek: " & $Modified_iDayOfWeek & @CRLF)
    ConsoleWrite("-DayName  : " & $Modified_iDayName & @CRLF)
    ConsoleWrite("-MonthName: " & $Modified_iMonthName & @CRLF)

    Local $Accessed_ArrayFileTime = FileGetTime($sFilePath, $FT_ACCESSED) ; $FT_ACCESSED (2) = Last accessed
    Local $Accessed_iYear = $Accessed_ArrayFileTime[0] ; = year (four digits)
    Local $Accessed_iMonth = $Accessed_ArrayFileTime[1] ; = month (range 01 - 12)
    Local $Accessed_iDay = $Accessed_ArrayFileTime[2] ; = day (range 01 - 31)
    Local $Accessed_iHour = $Accessed_ArrayFileTime[3] ; = hour (range 00 - 23)
    Local $Accessed_iMin = $Accessed_ArrayFileTime[4] ; = min (range 00 - 59)
    Local $Accessed_iSec = $Accessed_ArrayFileTime[5] ; = sec (range 00 - 59)
    Local $Accessed_iDayOfWeek = _DateToDayOfWeek($Accessed_iYear, $Accessed_iMonth, $Accessed_iDay)
    Local $Accessed_iDayName = _DateDayOfWeek($Accessed_iDayOfWeek)
    Local $Accessed_iMonthName = _DateToMonth($Accessed_iMonth)
    Local $Accessed_iShortTime = $Accessed_iYear & $Accessed_iMonth & $Accessed_iDay & $Accessed_iHour & $Accessed_iMin & $Accessed_iSec
    Local $Accessed_iLongTime = $Accessed_iYear & "/" & $Accessed_iMonth & "/" & $Accessed_iDay & " - " & $Accessed_iHour & ":" & $Accessed_iMin & ":" & $Accessed_iSec
    Local $Accessed_iLongTimeName = $Accessed_iDayName & "," & $Accessed_iMonthName & " " & $Accessed_iDay & "," & $Accessed_iYear
    ConsoleWrite("! File   : " & $sFilePath & @CRLF)
    ConsoleWrite("-DATE Accessed  : " & $Accessed_iLongTimeName & @CRLF)
    ConsoleWrite(">DATE Accessed S: " & $Accessed_iLongTime & @CRLF)
    ConsoleWrite("+DATE Accessed L: " & $Accessed_iShortTime & @CRLF)
    ConsoleWrite("-Year     : " & $Accessed_iYear & @CRLF)
    ConsoleWrite("-Mont     : " & $Accessed_iMonth & @CRLF)
    ConsoleWrite("-Day      : " & $Accessed_iDay & @CRLF)
    ConsoleWrite("-Hour     : " & $Accessed_iHour & @CRLF)
    ConsoleWrite("-Min      : " & $Accessed_iMin & @CRLF)
    ConsoleWrite("-Sec      : " & $Accessed_iSec & @CRLF)
    ConsoleWrite("-DayOfWeek: " & $Accessed_iDayOfWeek & @CRLF)
    ConsoleWrite("-DayName  : " & $Accessed_iDayName & @CRLF)
    ConsoleWrite("-MonthName: " & $Accessed_iMonthName & @CRLF)

    
EndFunc   ;==>_FileGetTime

 

Regards,
 

Link to comment
Share on other sites

  • 4 weeks later...

Thanks guys for the answers, took me too long to notice because I was supposed to be notified by email.

I was going to test VIP's code to debug, but the problem has somehow resolved itself. The exact same code of mine now returns the correct date and parses it without problems... A very strange edge-case. I don't think it was a lock problem like careca said, because these files shouldn't have been accessed by any programs and my tool opened them in read-mode anyway (to fetch the date).

In any way, I developed the code above in one go without reboots, maybe that fixed it. Windows 7 SP1 64-bit, NTFS. /thread

Link to comment
Share on other sites

  • 3 months later...
On 11/11/2018 at 7:19 PM, careca said:

Other than that, the file is in use by some program that locks it or something, or is in read mode.

Solved the mystery:

As careca said, THE FILE WAS LOCKED. Although I opened the file in READ-ONLY in my code and the file was also opened READ-ONLY by MediaPlayerClassic -  apparently it was enough for Windows to deny file access to my AutoIt script... Damn!

I also found out why:

The _WinAPI_CreateFile() doc states: _WinAPI_CreateFile ( $sFileName, $iCreation [, $iAccess = 4 [, $iShare = 0 [, $iAttributes = 0 [, $tSecurity = 0]]]] )

Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only

Do you see it? Although I opened the file as read-only, the DEFAULT $iShare access in Au3 is set to NOT SHARE (==0).

Quote
$iShare [optional] Sharing mode of an object:
    1 - Delete
    2 - Read
    4 - Write

So if you open the file in really read-only, to get it's metadata, you want to allow ANY kind of access. Be it another read-only, write or delete:

Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2, 7) ; read-only and share any

Now the file can be opened by my media player and I can retrieve its metadata in background! I didn't know a read-only access had to be allowed in the API call, I just assumed it by default.

Edited by VADemon
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

×
×
  • Create New...