SetFileTime Function

Declare Function SetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

Platforms

Description & Usage

SetFileTime sets the creation, last-accessed, and last-modified (last written-to) dates and times associated with a file. All the times are in UTC time (Coordinated Universal Time, a.k.a. Greenwich Mean Time (GMT)), not in the system's local time. The times actually stored on the system may vary slightly from the times passed to the function because file times are not stored with perfect resolution (for example, seconds data may be ignored).

Return Value

If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a non-zero value.

Visual Basic-Specific Issues

None.

Parameters

hFile
A handle to the opened file to set the times of. The file must have been opened with at least write-level access.
lpCreationTime
The date and time to set as the file's creation time.
lpLastAccessTime
The date and time to set as the file's last access time.
lpLastWriteTime
The date and time to set as the file's last write-to (modification) time.

Example

' This code is licensed according to the terms and conditions listed here.

' Set the modification time of C:\MyApp\test.txt to
' the current system date and time.  Leave the other times as they
' were before calling the function.
Dim hFile As Long  ' handle to the opened file
Dim ctime As FILETIME  ' the time of creation
Dim atime As FILETIME  ' the time of last access
Dim mtime As FILETIME  ' the time of last modification
Dim retval As Long  ' return value

' First, open the file C:\MyApp\test.txt for both read-level and
' write-level access, since we need to do both.
hFile = CreateFile("C:\MyApp\test.txt", GENERIC_READ Or GENERIC_WRITE, 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)

' Get the system time (already in UTC) as a FILETIME structure.
GetSystemTimeAsFileTime mtime
' Set the retrieved creation and access times and the new modification
' time as the file's times.
retval = SetFileTime(hFile, ctime, atime, mtime)

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

See Also

GetFileTime

Category

Files

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 2, 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/s/setfiletime.html