We help IT Professionals succeed at work.

API GetFileTime does not return file hour, minute, second

laurent_diep
laurent_diep asked
on
Medium Priority
645 Views
Last Modified: 2008-02-01
Hello,

I try to use the GetFileTime function for me to check file hour, minutes and seconds in a VB application.

I succeed to retrieve the file date but the hour, lminute, second are false.
(It retrieves me something but not the good values.)

Have you already use the GetFileTime function ?

Here is my code :

**********************************************************
' Generic access rights extracted from WinNT.h
Private Const GENERIC_ALL = &H10000000
Private Const GENERIC_EXECUTE = &H20000000
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3

Public Const MAX_PATH = 260
Public Const MAXDWORD = &HFFFF
Public Const INVALID_HANDLE_VALUE = -1
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100

'Private Const GENERIC_READ = &H80000000
'      Private Const OPEN_EXISTING = 3
      Private Const OPEN_ALWAYS = 4
      Private Const FILE_SHARE_READ = &H1
      Private Const FILE_SHARE_WRITE = &H2
      'Private Const FILE_ATTRIBUTE_NORMAL = &H80

Type FILETIME
  dwLowDateTime As Long
  dwHighDateTime As Long
End Type

Type SYSTEMTIME
  wYear As Integer
  wMonth As Integer
  wDayOfWeek As Integer
  wDay As Integer
  wHour As Integer
  wMinute As Integer
  wSecond As Integer
  wMilliseconds As Integer
End Type

Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Declare Function FileTimeToLocalFileTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Declare Function FileTimeToSystemTime Lib "kernel32.dll" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

Sub essai11()

' Display the date on which the file C:\MyApp\test.txt was
' created.  Note how the time zone conversion is necessary.
Dim hFile As Long  ' handle to the opened file
Dim ctime As FILETIME  ' receives time of creation
Dim atime As FILETIME  ' receives time of last access
Dim mtime As FILETIME  ' receives time of last modification
Dim thetime As SYSTEMTIME  ' used to manipulate the time
Dim retval As Long  ' return value

' First, open the file C:\MyApp\test.txt for read-level access.  Note the
' expression necessary to pass 0 as lpSecurityAttributes.
hFile = CreateFile("d:\20011119\bd1.mdb", GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hFile = -1 Then
  Debug.Print "Could not open the file successfully -- aborting."
  End  ' terminate the program
End If

'Next, get the creation, last-access, and last-modification times.
retval = GetFileTime(hFile, ctime, atime, mtime)
' Convert the creation time to the local time zone.
retval = FileTimeToLocalFileTime(ctime, ctime)
' Convert the FILETIME format to the SYSTEMTIME format.
retval = FileTimeToSystemTime(ctime, thetime)

' Display the date of creation of the file to the user.
Debug.Print "The file was created on "; thetime.wMonth; "-"; thetime.wDay; "-"; thetime.wYear
Debug.Print "Heure minute seconde "; thetime.wHour; "-"; thetime.wMinute; "-"; thetime.wSecond

' Close the file to free up resources.
retval = CloseHandle(hFile)

End Sub

*****************************************************

Thank you very much to help.

Laurent_Diep

Comment
Watch Question

Author

Commented:
Hello,

I did not check with enough accuracy : in fact in returns to me the creation date and hour but I would like to obtain the last update date and hour.

Thank you.

Laurent_Diep.

Author

Commented:
Hello,

Sorry, I found what was wrong :

I had to choose mtime instead of ctime.

Sorry again.
Laurent_Diep.

Commented:
Try this code, it might help you.
1) create a standard exe
2) add this code
3) run the project
Good luck!
dannic

Option Explicit
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Declare Function CreateFile Lib _
    "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
    ByVal NoSecurity As Long, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib _
    "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetFileTime Lib _
    "kernel32" (ByVal hFile As Long, _
    lpCreationTime As FILETIME, _
    lpLastAccessTime As FILETIME, _
    lpLastWriteTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib _
    "kernel32" (lpFileTime As FILETIME, _
    lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib _
    "kernel32" (lpFileTime As FILETIME, _
    lpLocalFileTime As FILETIME) As Long
   
Private Const GENERIC_READ = &H80000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3
Private Const INVALID_HANDLE_VALUE = -1
Function GetFileTimeInfo(ByVal FileName As String, Optional CreateDate As Date, _
    Optional ModifyDate As Date, Optional LastAccessDate As Date) As Boolean

    Dim hFile As Long
    Dim ftCreate As FILETIME
    Dim ftModify As FILETIME
    Dim ftLastAccess As FILETIME
    Dim ft As FILETIME
    Dim st As SYSTEMTIME

    hFile = CreateFile(FileName, GENERIC_READ, _
        FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
    If hFile = INVALID_HANDLE_VALUE Then Exit Function

    If GetFileTime(hFile, ftCreate, ftLastAccess, ftModify) Then
       
        GetFileTimeInfo = True
       

        FileTimeToLocalFileTime ftCreate, ft
       
        FileTimeToSystemTime ft, st
       
        CreateDate = DateSerial(st.wYear, st.wMonth, _
            st.wDay) + TimeSerial(st.wHour, st.wMinute, _
            st.wSecond) + (st.wMilliseconds / 86400000)

        FileTimeToLocalFileTime ftModify, ft
        FileTimeToSystemTime ft, st
        ModifyDate = DateSerial(st.wYear, st.wMonth, _
            st.wDay) + TimeSerial(st.wHour, st.wMinute, _
            st.wSecond) + (st.wMilliseconds / 86400000)

        FileTimeToLocalFileTime ftLastAccess, ft
        FileTimeToSystemTime ft, st
        LastAccessDate = DateSerial(st.wYear, st.wMonth, _
            st.wDay) + TimeSerial(st.wHour, st.wMinute, _
            st.wSecond) + (st.wMilliseconds / 86400000)
    End If
   

    CloseHandle hFile

End Function



Private Sub Form_Load()
Dim Created As Date, Modified As Date, Accessed As Date
Dim sDescr As String

GetFileTimeInfo "C:\autoexec.bat", Created, Modified, Accessed

sDescr = "Time info for autoexec.bat:" & vbCrLf
sDescr = sDescr & "Create date: " & Created & vbCrLf
sDescr = sDescr & "Modify date: " & Modified & vbCrLf
sDescr = sDescr & "Last access date: " & Accessed & vbCrLf

MsgBox sDescr
End Sub

Author

Commented:
dannic :

I am sorry but a the same time I posted this question, I found what was wrong.

Si, this question is canceled.

Thank you to have answered.

Laurent_Diep.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.