Announcement

Collapse
No announcement yet.

App self date-time stamp

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Michael Mattias
    replied
    Just a note here... Both CreateFile() and GetFileInformationByHandle() require you to succesfully OPEN the file to get the info...which is not possible if another application has opened the file in an exclusive mode.

    FindFirstFile OTOH does not require an OPEN and will always return the info.

    MCM

    Leave a comment:


  • Pierre Bellisle
    replied
    Paulo,
    here are 3 pieces of compilable code, first one use GetFileTime,
    second one use FindFirstFile and the last one use GetFileInformationByHandle.

    Code:
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc" '#2005-01-27#
    '______________________________________________________________________________
     
    FUNCTION GetDateTimeFormat(FileDate AS FILETIME) AS STRING
     LOCAL LocalDate  AS FILETIME
     LOCAL SystemDate AS SYSTEMTIME
     
     FileTimeToLocalFileTime FileDate, LocalDate 'Convert from UTC to local time
     FileTimeToSystemTime LocalDate, SystemDate 'Convert to human readeable format
     
     FUNCTION = _ 'Format the resulting date, time and weekday
       FORMAT$(SystemDate.wyear, "0000") & "/" & FORMAT$(SystemDate.wMonth,  "00") & "/" & _
       FORMAT$(SystemDate.wDay,    "00") & " " & FORMAT$(SystemDate.wHour,   "00") & ":" & _
       FORMAT$(SystemDate.wMinute, "00") & ":" & FORMAT$(SystemDate.wSecond, "00") & " " & _
       "(" & FORMAT$(SystemDate.wMilliSeconds, "000") & " milliseconds) " & _
       CHOOSE$(SystemDate.wDayOfWeek + 1, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION PBMAIN()
     LOCAL zFileName      AS ASCIIZ * %Max_Path
     LOCAL FileNameLen    AS LONG
     LOCAL hFile          AS DWORD
     LOCAL CreationTime   AS FILETIME
     LOCAL LastAccessTime AS FILETIME
     LOCAL LastWriteTime  AS FILETIME
     
     FileNameLen = GetModuleFileName(BYVAL %NULL, zFileName, %Max_Path) 'Get this exe file name
     
     hFile = CreateFile(zFileName, %GENERIC_READ, %FILE_SHARE_READ, _ 'Get a file handle so we can query
                        BYVAL 0, %OPEN_EXISTING, %FILE_ATTRIBUTE_NORMAL, 0)
     IF hFile <> %INVALID_HANDLE_VALUE THEN 'Check if handle is valid
       GetFileTime hFile, CreationTime, LastAccessTime, LastWriteTime 'Get dates info
       MessageBox %HWND_DESKTOP, zFileName          & $CRLF & $CRLF & _
                  "CreationTime"                            & $CRLF & _
                  GetDateTimeFormat(CreationTime)   & $CRLF & $CRLF & _
                  "LastAccessTime"                          & $CRLF & _
                  GetDateTimeFormat(LastAccessTime) & $CRLF & $CRLF & _
                  "LastWriteTime"                           & $CRLF & _
                  GetDateTimeFormat(LastWriteTime), _
                  "Time with mili-second and weekday", %MB_ICONINFORMATION OR %MB_OK
       CloseHandle hFile 'Release handle clean-up
     END IF
     
    END FUNCTION
    '______________________________________________________________________________
    '
    Code:
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc" '#2005-01-27#
    '______________________________________________________________________________
     
    FUNCTION GetDateTimeFormat(FileDate AS FILETIME) AS STRING
     LOCAL LocalDate  AS FILETIME
     LOCAL SystemDate AS SYSTEMTIME
     
     FileTimeToLocalFileTime FileDate, LocalDate 'Convert from UTC to local time
     FileTimeToSystemTime LocalDate, SystemDate 'Convert to human readeable format
     
     FUNCTION = _ 'Format the resulting date, time and weekday
       FORMAT$(SystemDate.wyear, "0000") & "/" & FORMAT$(SystemDate.wMonth,  "00") & "/" & _
       FORMAT$(SystemDate.wDay,    "00") & " " & FORMAT$(SystemDate.wHour,   "00") & ":" & _
       FORMAT$(SystemDate.wMinute, "00") & ":" & FORMAT$(SystemDate.wSecond, "00") & " " & _
       "(" & FORMAT$(SystemDate.wMilliSeconds, "000") & " milliseconds) " & _
       CHOOSE$(SystemDate.wDayOfWeek + 1, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION PBMAIN()
     LOCAL zFileName   AS ASCIIZ * %Max_Path
     LOCAL FileNameLen AS LONG
     LOCAL hFile       AS DWORD
     LOCAL FileInfo    AS WIN32_FIND_DATA
     
     FileNameLen = GetModuleFileName(BYVAL %NULL, zFileName, %Max_Path) 'Get this exe file name
     
     hFile = FindFirstFile(zFileName, FileInfo) 'Get file info
     IF hFile <> %INVALID_HANDLE_VALUE THEN 'Check if handle is valid
       MessageBox %HWND_DESKTOP, zFileName                     & $CRLF & $CRLF & _
                  "CreationTime"                                       & $CRLF & _
                  GetDateTimeFormat(FileInfo.ftCreationTime)   & $CRLF & $CRLF & _
                  "LastAccessTime"                                     & $CRLF & _
                  GetDateTimeFormat(FileInfo.ftLastAccessTime) & $CRLF & $CRLF & _
                  "LastWriteTime"                                      & $CRLF & _
                  GetDateTimeFormat(FileInfo.ftLastWriteTime), _
                  "Time with mili-second and weekday", %MB_ICONINFORMATION OR %MB_OK
       FindClose hFile 'Release handle clean-up
     END IF
     
    END FUNCTION
    '______________________________________________________________________________
    '
    Code:
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc" '#2005-01-27#
    '______________________________________________________________________________
     
    FUNCTION GetDateTimeFormat(FileDate AS FILETIME) AS STRING
     LOCAL LocalDate  AS FILETIME
     LOCAL SystemDate AS SYSTEMTIME
     
     FileTimeToLocalFileTime FileDate, LocalDate 'Convert from UTC to local time
     FileTimeToSystemTime LocalDate, SystemDate 'Convert to human readeable format
     
     FUNCTION = _ 'Format the resulting date, time and weekday
       FORMAT$(SystemDate.wyear, "0000") & "/" & FORMAT$(SystemDate.wMonth,  "00") & "/" & _
       FORMAT$(SystemDate.wDay,    "00") & " " & FORMAT$(SystemDate.wHour,   "00") & ":" & _
       FORMAT$(SystemDate.wMinute, "00") & ":" & FORMAT$(SystemDate.wSecond, "00") & " " & _
       "(" & FORMAT$(SystemDate.wMilliSeconds, "000") & " milliseconds) " & _
       CHOOSE$(SystemDate.wDayOfWeek + 1, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION PBMAIN()
     LOCAL zFileName   AS ASCIIZ * %Max_Path
     LOCAL FileNameLen AS LONG
     LOCAL hFile       AS DWORD
     LOCAL FileInfo    AS BY_HANDLE_FILE_INFORMATION
     
     FileNameLen = GetModuleFileName(BYVAL %NULL, zFileName, %Max_Path) 'Get this exe file name
     
     hFile = CreateFile(zFileName, %GENERIC_READ, %FILE_SHARE_READ, _ 'Get a file handle so we can query
                        BYVAL 0, %OPEN_EXISTING, %FILE_ATTRIBUTE_NORMAL, 0)
     IF hFile <> %INVALID_HANDLE_VALUE THEN 'Check if handle is valid
       GetFileInformationByHandle hFile, FileInfo
       MessageBox %HWND_DESKTOP, zFileName                     & $CRLF & $CRLF & _
                  "CreationTime"                                       & $CRLF & _
                  GetDateTimeFormat(FileInfo.ftCreationTime)   & $CRLF & $CRLF & _
                  "LastAccessTime"                                     & $CRLF & _
                  GetDateTimeFormat(FileInfo.ftLastAccessTime) & $CRLF & $CRLF & _
                  "LastWriteTime"                                      & $CRLF & _
                  GetDateTimeFormat(FileInfo.ftLastWriteTime), _
                  "Time with mili-second and weekday", %MB_ICONINFORMATION OR %MB_OK
       CloseHandle hFile 'Release handle clean-up
     END IF
     
    END FUNCTION
    '______________________________________________________________________________
    '
    Last edited by Pierre Bellisle; 22 Jan 2008, 10:15 PM.

    Leave a comment:


  • Michael Mattias
    replied
    You don't need to call GetFileTime.

    The Win32_FIND_DATA structure returned by FindFirstFile includes the file times!

    Ok, I give up. Here's a function you can use...(I can't believe I am wimping out like this)


    Code:
    FUNCTION FileInformation (szFileName AS ASCIIZ , szFileSize AS ASCIIZ, szCreateDate AS ASCIIZ,_
                              szCreateTime AS ASCIIZ, szModifyDate AS ASCIIZ, szModifyTime AS ASCIIZ) AS LONG
        ' returns: 0 = success, else error
        LOCAL szDF  AS ASCIIZ * 36, szTF AS ASCIIZ * 36
        LOCAL szSizeFormat AS ASCIIZ * 16, szDateFormat AS ASCIIZ * 36, szTimeFormat AS ASCIIZ * 36
        LOCAL hSearch AS LONG          '<< Note LONG not DWORD
        LOCAL W32 AS WIN32_FIND_DATA, systime AS SYSTEMTIME, ft AS FILETIME
    
        LOCAL DateBufferSize AS LONG, SizeBufferSize AS LONG
    
        DateBufferSize = 16
        SizeBufferSize = 12
    
    
        szDF         = "MM'/'dd'/'yy"     ' the formats in which dates and times are returned
        szTF         = "hh':'mm':'ss tt"
        szSizeFormat = "###,###,###"
    
        ' null all the passed parameters
        szFileSize   = ""
        szModifyDate = ""
        szModifyTime = ""
        szCreateDate = ""
        szCreateTime = ""
        FUNCTION = %TRUE  ' default = failure
       ' if file IS available get file info...
       ' get formatted date and time
        hSearch        = FindFirstFile(szFilename, W32)
        IF  hSearch  <> %INVALID_HANDLE_VALUE_LONG THEN      ' the file was found, info is in W32
               szFileSize   = FORMAT$(W32.nFileSizeLow, szSizeFormat)
               FileTimeToLocalFileTime W32.ftLastWriteTime, ft
               FileTimeToSystemTime ft, SysTime
               GetDateFormat BYVAL %NULL, BYVAL %NULL, SysTime, szDf, szModifyDate, DateBufferSize
               GetTimeFormat BYVAL %NULL, BYVAL %NULL, SysTime, szTF, szModifyTime, DateBufferSize
               FileTimetoLocalFileTime W32.ftCreationTime, ft
               FileTimeToSystemTime ft, SysTime
               GetDateFormat BYVAL %NULL, BYVAL %NULL, SysTime, szDf, szCreateDate, DateBUfferSize
               GetTimeFormat BYVAL %NULL, BYVAL %NULL, SysTime, szTF, szCreateTime, DateBufferSize
               FindClose hSearch
               FUNCTION = %FALSE   ' success!
        END IF  ' if we even found the file
    
    END FUNCTION

    Leave a comment:


  • Adam J. Drake
    replied
    With GetFileTime, you also have to open the file - FindFirstFile is your easiest avenue to retrieve all the information you want about the file...

    Leave a comment:


  • Paulo Ferreira
    replied
    Sure, but where is app handle (hApp) to
    CALL GetFileTime (hApp, lpCreationTime, lpLastAccessTime, lpLastWriteTime) ?

    Leave a comment:


  • Michael Mattias
    replied
    There's no reason to OPEN the file just to get its size .....

    Code:
    LOCAL szProg AS ASCIIZ * %MAX_ PATH , W32 AS WIN32_FIND_DATA, h AS LONG
    
       GetModuleFileName BYVAL %NULL, szProg, SIZEOF(szProg)
       h = FindFirstFile (szProg, W32)
       IF h <> %INVALID_HANDLE_VALUE    THEN ' which it never will
           Print/MSGbox USING$ ("Executable file '&' is size #,", szProg, W32.nFileSizeLow)
           FindClose h
       ELSE
           Should never happen message
       END IF
    (No, you don't need to worry about W32.nFileSizeHigh, since an EXE > 2 Gb will never execute).

    (Define h AS DWORD if your Win32API.INC casts INVALID_HANDLE_VALUE as DWORD).

    [added]
    DUH, you didn't want the size, you wanted the timestamp. Well, that's in the WIN32_FIND_DATA structure too. Just format it
    [/added]

    MCM
    Last edited by Michael Mattias; 21 Jan 2008, 10:48 AM. Reason: Added DUH.

    Leave a comment:


  • Paulo Ferreira
    replied
    Ok.
    Now I test this option too:

    ...
    LOCAL lpFileName AS ASCIIZ * 260 '--> this exe file
    LOCAL lpReOpenBuff AS OFSTRUCT
    LOCAL hFile AS LONG
    hFile = OpenFile(lpFileName, lpReOpenBuff, %OF_SHARE_COMPAT)
    ...

    %OF_SHARE_COMPAT -> retrieve hFile and lpReOPenBuff of this exe file,
    needed to get time stamp.

    Leave a comment:


  • Michael Mattias
    replied
    By getting its own file name then getting the size of that file.

    (Hint: GetModuleFileName, FindFirstFile )

    Leave a comment:


  • Paulo Ferreira
    started a topic App self date-time stamp

    App self date-time stamp

    How the app can get its own date-time stamp ?
    Thanks in advance.
Working...
X