Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
GetFileTime determines the times and dates of creation, last access, and last modification (write-to) of a file. Each of these times are placed in the corresponding structures passed to the function. All times obtained by the function are in UTC time (Coordinated Universal Time, a.k.a. Greenwich Mean Time (GMT)), not in the system's local time. Note that, depending on the operating system, the exact resolution of file times may vary -- the file times obtained by the function may not correspond to the actual date and time of creation/access/modification simply because the operating system does not store the information that precisely.
If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a non-zero value.
None.
' This code is licensed according to the terms and conditions listed here.
' 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("C:\MyApp\test.txt", 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
' Close the file to free up resources.
retval = CloseHandle(hFile)
GetFileInformationByHandle, SetFileTime
Go back to the alphabetical Function listing.
Go back to the Reference section index.
Last Modified: October 1, 1999
This page is copyright © 1999 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getfiletime.html