|
|
Title | Set a file's creation, last access, and last modified times |
Description | This example shows how to set a file's creation, last access, and last modified times in Visual Basic 6. It uses the GetFileTime and SetFileTime API functions. |
Keywords | file times, last access time, file creation time, lastmodified time |
Categories | Files and Directories |
|
|
Thanks to Timothy Ward for finding a bug in the SetFileTimes subroutine. (It was using GENERIC_READ instead of GENERIC_WRITE to open the file.)
Function GetFileTimes uses the GetFileTime API function to read the file's times. If necessary, it uses the FileTimeToLocalFileTime API function to convert the results into local times.
|
|
' Return True if there is an error.
Private Function GetFileTimes(ByVal file_name As String, _
ByRef creation_date As Date, ByRef access_date As Date, _
ByRef modified_date As Date, ByVal local_time As _
Boolean) As Boolean
Dim file_handle As Long
Dim creation_filetime As FILETIME
Dim access_filetime As FILETIME
Dim modified_filetime As FILETIME
Dim file_time As FILETIME
' Assume something will fail.
GetFileTimes = True
' Open the file.
file_handle = CreateFile(file_name, GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
0&, OPEN_EXISTING, 0&, 0&)
If file_handle = 0 Then Exit Function
' Get the times.
If GetFileTime(file_handle, creation_filetime, _
access_filetime, modified_filetime) = 0 Then
CloseHandle file_handle
Exit Function
End If
' Close the file.
If CloseHandle(file_handle) = 0 Then Exit Function
' See if we should convert to the local
' file system time.
If local_time Then
' Convert to local file system time.
FileTimeToLocalFileTime creation_filetime, file_time
creation_filetime = file_time
FileTimeToLocalFileTime access_filetime, file_time
access_filetime = file_time
FileTimeToLocalFileTime modified_filetime, file_time
modified_filetime = file_time
End If
' Convert into dates.
creation_date = FileTimeToDate(creation_filetime)
access_date = FileTimeToDate(access_filetime)
modified_date = FileTimeToDate(modified_filetime)
GetFileTimes = False
End Function
|
|
Function SetFileTimes converts local times into file times. See the code to learn how DateToFileTime works. It then uses the SetFileTime API function to set the file's times.
|
|
' Return True if there is an error.
Private Function SetFileTimes(ByVal file_name As String, _
ByVal creation_date As Date, ByVal access_date As Date, _
ByVal modified_date As Date, ByVal local_times As _
Boolean) As Boolean
Dim file_handle As Long
Dim creation_filetime As FILETIME
Dim access_filetime As FILETIME
Dim modified_filetime As FILETIME
Dim file_time As FILETIME
' Assume something will fail.
SetFileTimes = True
' Convert the dates into FILETIMEs.
creation_filetime = DateToFileTime(creation_date)
access_filetime = DateToFileTime(access_date)
modified_filetime = DateToFileTime(modified_date)
' Convert the file times into system file times.
If local_times Then
LocalFileTimeToFileTime creation_filetime, file_time
creation_filetime = file_time
LocalFileTimeToFileTime access_filetime, file_time
access_filetime = file_time
LocalFileTimeToFileTime modified_filetime, file_time
modified_filetime = file_time
End If
' Open the file.
file_handle = CreateFile(file_name, GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
0&, OPEN_EXISTING, 0&, 0&)
If file_handle = 0 Then Exit Function
'creation_date = FileTimeToDate(creation_filetime)
' Set the times.
If SetFileTime(file_handle, creation_filetime, _
access_filetime, modified_filetime) = 0 Then
CloseHandle file_handle
Exit Function
End If
' Close the file.
If CloseHandle(file_handle) = 0 Then Exit Function
SetFileTimes = False
End Function
|
|
See the code for other details such as time format conversions.
|
|
|
|
|
|