Sign in to follow this  
Followers 0
JohnOne

FileGetTime problem

21 posts in this topic

Here is the code

#include <file.au3>
;#include <Array.au3>

_FileWriteLog(@ScriptDir & "\my.log.txt", "")

Sleep(1000)

$t = FileGetTime(@ScriptDir & "\my.log.txt", 1, 0)
If IsArray($t) Then
    MsgBox(0, "created on", $t[4])
Else
    MsgBox(0, "Error", "Error getting time")
EndIf
;_ArrayDelete($t, 4)

This just writes a logfile containing just the timestamp

Gets the time the file was created and writes to a 6 element array ($t)

[4] of that array is the minutes, an thats what I'm trying to display in the msgbox.

I first ran the script at 19:50 and the msgbox displayed "50" as I expected

My problem is that, even after I delete the logfile from the script folder and run the script again, it still returns "50" even though a new logfile was created.

Even after closing windows explorer, and Scite editor and restarting them both.

Any ideas why this may be happening ?


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



I get the same thing. I displayed the minutes and the seconds and once I had created the file the same time was shown even if I deleted it and created it again. I tried 4 times. I have no idea why but if you try the same thing in a sub folder then it doesn't happen.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

because the file is cached by windows? If you just recreate it then Windows probably used the cached data.

However on reflection and a bit of testing this seems to work just fine.

$sFile = @DesktopDir & "\test1.txt"
FileWrite($sFile, "")
MsgBox(0, "Test", _GetMin())
FileDelete($sFile)
Sleep(120000)
FileWrite($sFile, "")
MsgBox(0, "Test 2", _GetMin())

Func _GetMin()
    $sTime = FileGetTime($sFile, 1, 1)
    $sMin = StringMid($sTime, 11, 2)
    ;; Or the SRE replace method
    ;$sMin = StringRegExpReplace($sTime, "^\d{10}(\d{2})\d+$", "$1")
    Return $sMin
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks very much

I dont know about all the SRER stuff but stringmid or using another folder should work for my purpose

Cheers again

EDIT: for referance, the folder option worked first time, but then acted the same.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Using the example script above, I tried this, which is acting in the same manner as the original problem :)

#include <file.au3>
$sFile = @ScriptDir & "\test1.txt"
FileWrite($sFile, "")
MsgBox(0, "Test", _GetMin())

Func _GetMin()
    $sTime = FileGetTime($sFile, 1, 1)
    $sMin = StringMid($sTime, 11, 2)
    Return $sMin
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Using the example script above, I tried this, which is acting in the same manner as the original problem :)

#include <file.au3>
$sFile = @ScriptDir & "\test1.txt"
FileWrite($sFile, "")
MsgBox(0, "Test", _GetMin())

Func _GetMin()
    $sTime = FileGetTime($sFile, 1, 1)
    $sMin = StringMid($sTime, 11, 2)
    Return $sMin
EndFunc

Interesting because it didn't fail with the FileDelete() in there as shown in the original code.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

It seems a little strange, I've been trying to slot a ByRef in there, but with no luck.

The code works once, in each new folder you ask it to create the file, so it will display "20" after deleting will display "21" but then "21" thereafter

The thing is, I'm going to need to keep the file intact in my script.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I think you were correct with your windows caching point

Even if I manually create an empty .txt file in windows with the same test1.txt name it reports the create time as the earlier one.

But If I delete it, refresh the folder and create another with same name, it reports the new time


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I did some more checking and it's definitly the Windows cache. I don't know of any efficient methods of clearin that in a script. I did notice though that the last modified time changes when you re-create the file so you may want to see if you can do anything with that.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Hmmm, interesting topic...

Maybe it works when the log-file is read and written with a tweaked _WinAPI_CreateFile() function?

Looking at MSDN

http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx#caching_behavior

I found the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags which would need to be added to _WinAPI_CreateFile() to disable file caching. Maybe this solves the behavior?

Edited by KaFu

Share this post


Link to post
Share on other sites

Hmmm, interesting topic...

Maybe it works when the log-file is read and written with a tweaked _WinAPI_CreateFile() function?

Looking at MSDN

http://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx#caching_behavior

I found the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags which would need to be added to _WinAPI_CreateFile() to disable file caching. Maybe this solves the behavior?

That looks good KaFu, so I tried it.

#AutoIt3Wrapper_Add_Constants=n
Global $FILE_FLAG_NO_BUFFERING = 0x20000000, $FILE_FLAG_WRITE_THROUGH = 0x80000000
#include <file.au3>
#include <constants.au3>
#include <winapi.au3>
;#include <Array.au3>

if not FileExists(@ScriptDir & "\my.log.txt") Then
    FileClose(_WinAPI_CreateFileB(@ScriptDir & "\my.log.txt",1,2))
EndIf
_FileWriteLog(@ScriptDir & "\my.log.txt", "")

Sleep(1000)

$t = FileGetTime(@ScriptDir & "\my.log.txt", 1, 0)
If IsArray($t) Then
    MsgBox(0, "created on", $t[4])
Else
    MsgBox(0, "Error", "Error getting time")
EndIf




Func _WinAPI_CreateFileB($sFileName, $iCreation, $iAccess = 4, $iShare = 0, $iAttributes = 0, $pSecurity = 0)
    Local $iDA = 0, $iSM = 0, $iCD = 0, $iFA = 0, $aResult

    If BitAND($iAccess, 1) <> 0 Then $iDA = BitOR($iDA, $__WINAPCONSTANT_GENERIC_EXECUTE)
    If BitAND($iAccess, 2) <> 0 Then $iDA = BitOR($iDA, $__WINAPCONSTANT_GENERIC_READ)
    If BitAND($iAccess, 4) <> 0 Then $iDA = BitOR($iDA, $__WINAPCONSTANT_GENERIC_WRITE)

    If BitAND($iShare, 1) <> 0 Then $iSM = BitOR($iSM, $__WINAPCONSTANT_FILE_SHARE_DELETE)
    If BitAND($iShare, 2) <> 0 Then $iSM = BitOR($iSM, $__WINAPCONSTANT_FILE_SHARE_READ)
    If BitAND($iShare, 4) <> 0 Then $iSM = BitOR($iSM, $__WINAPCONSTANT_FILE_SHARE_WRITE)

    Switch $iCreation
        Case 0
            $iCD = $__WINAPCONSTANT_CREATE_NEW
        Case 1
            $iCD = $__WINAPCONSTANT_CREATE_ALWAYS
        Case 2
            $iCD = $__WINAPCONSTANT_OPEN_EXISTING
        Case 3
            $iCD = $__WINAPCONSTANT_OPEN_ALWAYS
        Case 4
            $iCD = $__WINAPCONSTANT_TRUNCATE_EXISTING
    EndSwitch

    If BitAND($iAttributes, 1) <> 0 Then $iFA = BitOR($iFA, $__WINAPCONSTANT_FILE_ATTRIBUTE_ARCHIVE)
    If BitAND($iAttributes, 2) <> 0 Then $iFA = BitOR($iFA, $__WINAPCONSTANT_FILE_ATTRIBUTE_HIDDEN)
    If BitAND($iAttributes, 4) <> 0 Then $iFA = BitOR($iFA, $__WINAPCONSTANT_FILE_ATTRIBUTE_READONLY)
    If BitAND($iAttributes, 8) <> 0 Then $iFA = BitOR($iFA, $__WINAPCONSTANT_FILE_ATTRIBUTE_SYSTEM)

;add the extra flags to see what happens
    $iFA = BitOr($iFA,$FILE_FLAG_NO_BUFFERING, $FILE_FLAG_WRITE_THROUGH)

    $aResult = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", $sFileName, "int", $iDA, "int", $iSM, "ptr", $pSecurity, "int", $iCD, "int", $iFA, "int", 0)
    If @error Then Return SetError(@error, 0, 0)
    If $aResult[0] = 0xFFFFFFFF Then Return 0
    Return $aResult[0]
EndFunc ;==>_WinAPI_CreateFile

But it didn't work. The first time I deleted the file and ran the script the time changed, but after that it kept the same.

Maybe someone can see something wrong with what I did.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

While searching the OS issue on google, I came across this topic http://social.msdn.microsoft.com/Forums/en-IE/netfxbcl/thread/80c3aa83-ace8-4de3-b560-030ac70c1b8c.

Same issue and reproduced on different OS'

It seems the problem occurs in winxp, disappears in vista, and re-occurs in win7

Something to do with the way the OS indexes files maybe, I dont know, but I cant see microsoft caring really.

I appreciate your time looking into this.

Thanks.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

For simplicity sakes, and so I don't have to spend time searching that ya'll have apparently done already... I suppose you could always create a function that creates a unique random file name, then use FileMove() on it to make it "my.log.txt".


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

For simplicity sakes, and so I don't have to spend time searching that ya'll have apparently done already... I suppose you could always create a function that creates a unique random file name, then use FileMove() on it to make it "my.log.txt".

In the mdsn thread I read, someone tested something similar, by changing the name and or moving another file.

When it took on the name of the target file, it still returned the earlier original file creation time :)


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

The file is really being held in memory and I'm wondering if a script like this one might work in here. With some experimentation I was able to show that the file details are consistently flushed if the file was deleted and then the system restarted so just flushing it from memory at some point may have the same effect.

Func _ReduceMemory($ProcID = 0)
    ; Original version : w_Outer
    ; modified by Rajesh V R to include process ID
    ; modified by Prog@ndy
    Local $ai_Return, $ai_Handle
    If $ProcID <= 0 Then ; No process id specified - use current process instead.
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', -1)
        If @error Then Return SetError(1, 0, 0)
        Return $ai_Return[0]
    EndIf

    $ProcID = ProcessExists($ProcID)
    If $ProcID = 0 Then Return SetError(2, 0, 0)
    $ai_Handle = DllCall("kernel32.dll", 'ptr', 'OpenProcess', 'dword', 0x1f0fff, 'int', False, 'dword', $ProcID)
    If @error Or $ai_Handle[0] = 0 Then Return SetError(3, 0, 0)
    $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'ptr', $ai_Handle[0])
    If @error Then
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
        Return SetError(4, 0, 0)
    EndIf
    DllCall('kernel32.dll', 'int', 'CloseHandle', 'ptr', $ai_Handle[0])
    Return $ai_Return[0]

EndFunc   ;==>_ReduceMemory

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

After reading this

http://www.devcity.net/Articles/133/1/article.aspx

I came up with this

_FileWriteLogEx(@ScriptDir & "\my.log.txt", "")

MsgBox(0,"",FileGetTime(@ScriptDir & "\my.log.txt", 1, 1))

; #FUNCTION# ====================================================================================================================
; Name...........: _FileWriteLogEx
; Description ...: Writes current date,time and the specified text to a log file. 
;                  Sets timestamp of logfile explicitly (windows cache might produce unexpected results otherwise, see http://www.devcity.net/Articles/133/1/article.aspx)
; Syntax.........: _FileWriteLog($sLogPath, $sLogMsg[, $iFlag = -1])
; Parameters ....: $sFilePath - Path and filename of the file to be written to
;                  $sLogMsg   - Message to be written to the log file
;                  $iFlag     - [Optional] - Flag that defines if $sLogMsg will be written to the end of file, or to the begining.
;                  |If $iFlag = -1 (default) $sLogMsg will be written to the end of file.
;                  |If $iFlag <> -1 $sLogMsg will be written to begining of file.
; Return values .: Success - Returns a 1
;                  Failure - Returns a 0
;                  @Error  - 0 = No error.
;                  |1 = Error opening specified file
;                  |2 = File could not be written to
;                  |2 = File could not be closed
; Author ........: Jeremy Landes <jlandes at landeserve dot com>
; Modified.......: MrCreatoR - added $iFlag parameter, KaFu - added explicitly set of timestamp
; Remarks .......:
; Related .......: .FileOpen
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _FileWriteLogEx($sLogPath, $sLogMsg, $iFlag = -1)
    ;==============================================
    ; Local Constant/Variable Declaration Section
    ;==============================================
    Local $sDateNow, $sTimeNow, $sMsg, $iWriteFile, $hOpenFile, $iOpenMode = 1
    Local $iLogFileExisted = 0

    if FileExists($sLogPath) then $iLogFileExisted = 1

    $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
    $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
    $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg

    If $iFlag <> -1 Then
        $sMsg &= @CRLF & FileRead($sLogPath)
        $iOpenMode = 2
    EndIf

    $hOpenFile = FileOpen($sLogPath, $iOpenMode)
    If $hOpenFile = -1 Then Return SetError(1, 0, 0)

    $iWriteFile = FileWriteLine($hOpenFile, $sMsg)
    If $iWriteFile = -1 Then Return SetError(2, 0, 0)

    if FileClose($hOpenFile) = 0 then Return SetError(3, 0, 0)

    if $iLogFileExisted = 0 then FileSetTime($sLogPath, "", 1)
    FileSetTime($sLogPath, "")

    Return 1

EndFunc   ;==>_FileWriteLog

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

After reading this

http://www.devcity.net/Articles/133/1/article.aspx

I came up with this

_FileWriteLogEx(@ScriptDir & "\my.log.txt", "")

MsgBox(0,"",FileGetTime(@ScriptDir & "\my.log.txt", 1, 1))

; #FUNCTION# ====================================================================================================================
; Name...........: _FileWriteLogEx
; Description ...: Writes current date,time and the specified text to a log file. 
;        Sets timestamp of logfile explicitly (windows cache might produce unexpected results otherwise, see http://www.devcity.net/Articles/133/1/article.aspx)
; Syntax.........: _FileWriteLog($sLogPath, $sLogMsg[, $iFlag = -1])
; Parameters ....: $sFilePath - Path and filename of the file to be written to
; $sLogMsg - Message to be written to the log file
; $iFlag - [Optional] - Flag that defines if $sLogMsg will be written to the end of file, or to the begining.
; |If $iFlag = -1 (default) $sLogMsg will be written to the end of file.
; |If $iFlag <> -1 $sLogMsg will be written to begining of file.
; Return values .: Success - Returns a 1
; Failure - Returns a 0
; @Error - 0 = No error.
; |1 = Error opening specified file
; |2 = File could not be written to
; |2 = File could not be closed
; Author ........: Jeremy Landes <jlandes at landeserve dot com>
; Modified.......: MrCreatoR - added $iFlag parameter, KaFu - added explicitly set of timestamp
; Remarks .......:
; Related .......: .FileOpen
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _FileWriteLogEx($sLogPath, $sLogMsg, $iFlag = -1)
    ;==============================================
    ; Local Constant/Variable Declaration Section
    ;==============================================
    Local $sDateNow, $sTimeNow, $sMsg, $iWriteFile, $hOpenFile, $iOpenMode = 1
    Local $iLogFileExisted = 0

    if FileExists($sLogPath) then $iLogFileExisted = 1

    $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
    $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
    $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg

    If $iFlag <> -1 Then
        $sMsg &= @CRLF & FileRead($sLogPath)
        $iOpenMode = 2
    EndIf

    $hOpenFile = FileOpen($sLogPath, $iOpenMode)
    If $hOpenFile = -1 Then Return SetError(1, 0, 0)

    $iWriteFile = FileWriteLine($hOpenFile, $sMsg)
    If $iWriteFile = -1 Then Return SetError(2, 0, 0)

    if FileClose($hOpenFile) = 0 then Return SetError(3, 0, 0)

    if $iLogFileExisted = 0 then FileSetTime($sLogPath, "", 1)
    FileSetTime($sLogPath, "")

    Return 1

EndFunc ;==>_FileWriteLog

Excellent KaFu, the link explains and your script fixes the problem. This is definitely worth knowing about. Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Here's some more info on why this behaviour exists:

http://www.jroller.com/dschneller/entry/windows_date_created_timestamp_strangeness

BTW, this was discussed and resolved before in this thread,

http://www.autoitscript.com/forum/index.php?showtopic=98749

and George was even a participant!

Edit: Added the link to the other AutoIt thread

Edited by ResNullius

Share this post


Link to post
Share on other sites

Here's some more info on why this behaviour exists:

http://www.jroller.com/dschneller/entry/windows_date_created_timestamp_strangeness

BTW, this was discussed and resolved before in this thread,

http://www.autoitscript.com/forum/index.php?showtopic=98749

and George was even a participant!

Edit: Added the link to the other AutoIt thread

And I'm supposed to remember that far back? I'll just blame it on the soggy brain syndrom and you and I live close enough to each other that I'm reasonably sure you are aware of that of which what I speak.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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