CompareFileTime Function

Declare Function CompareFileTime Lib "kernel32.dll" (lpFileTime1 As FILETIME, lpFileTime2 As FILETIME) As Long

Platforms: Win 32s, Win 95/98, Win NT

CompareFileTime compares two times stored in FILETIME format. The function determines which of the two times, if any, comes before the other chronologically. If the first time is earlier than the second time, the function returns -1. If the two times are equal, the function returns 0. If the first time is later than the second time, the function returns 1.

lpFileTime1
The first of the two times to compare.
lpFileTime2
The second of the two times to compare.

Example:

' Determine if file C:\MyProgram\datafile.txt was created before
' Jan 5, 1999.  Note how CreateFile's alternate declare must be used under Win 95/98 --
' see that function's page for more information.
Dim hfile As Long  ' receives the handle to the file
Dim ctime As FILETIME  ' receives creation date and time of the file
Dim atime As FILETIME  ' receives last access date and time of the file
Dim wtime As FILETIME  ' receives last write-to date and time of the file
Dim jantime As SYSTEMTIME  ' will be set to Jan 5, 1999
Dim janfiletime As FILETIME  ' will receive analogous time as jantime
Dim comptimes As Long  ' receives comparison of ctime and janfiletime
Dim retval As Long  ' return value

' Get a handle to the file (note how the alternate declare is used):
hfile = CreateFileNS("C:\MyProgram\datafile.txt", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hfile = -1 Then  ' if the file could not be opened
  Debug.Print "Could not open the file C:\MyProgram\datafile.txt."
  End  ' abort the program
End If

' Get the various times and dates associated with the file:
retval = GetFileTime(hfile, ctime, atime, wtime)
' Load jantime with the date January 5, 1999 at midnight:
jantime.wMonth = 1: jantime.wDay = 5: jantime.wYear = 1999
jantime.wHour = 0: jantime.wMinute = 0: jantime.wSecond = 0
' Convert jantime into FILETIME format so it can be compared with ctime:
retval = SystemTimeToFileTime(jantime, janfiletime)
' Compare the two times and display the relation:
comptimes = CompareFileTime(ctime, janfiletime)
If comptimes = -1 Then Debug.Print "File was created before midnight, January 5, 1999."
If comptimes = 0 Then Debug.Print "File was created at midnight, January 5, 1999."
If comptimes = 1 Then Debug.Print "File was created after midnight, January 5, 1999."

' Close the file
retval = CloseHandle(hfile)

Category: Time

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


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
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/c/comparefiletime.html