Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet a file's creation, last access, and last write times
Keywordsfile, creation, access, write
CategoriesFiles and Directories, Windows, Software Engineering
 
Use the GetFileTime API function. This gives the file times in UTC (Universal Time Coordinates which is the same as GMT). If you want times in your local file system time, use the FileTimeToLocalFileTime API function to convert the values.

Then translate the results (which are in 100s of nanoseconds since January 1, 1601) into the date and time.

 
' Return True if there is an error.
Private Function GetFileTimes(ByVal file_name As String, _
    ByRef date_created As Date, ByRef date_accessed As _
    Date, ByRef date_written As Date, ByVal local_time As _
    Boolean) As Boolean
Dim file_handle As Long
Dim creation_time As FILETIME
Dim access_time As FILETIME
Dim write_time As FILETIME
Dim file_time As FILETIME

    ' 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
        GetFileTimes = True
        Exit Function
    End If

    ' Get the times.
    If GetFileTime(file_handle, creation_time, _
        access_time, write_time) = 0 _
    Then
        GetFileTimes = True
        Exit Function
    End If

    ' Close the file.
    If CloseHandle(file_handle) = 0 Then
        GetFileTimes = True
        Exit Function
    End If

    ' See if we should convert to the local
    ' file system time.
    If local_time Then
        ' Convert to local file system time.
        FileTimeToLocalFileTime creation_time, file_time
        creation_time = file_time

        FileTimeToLocalFileTime access_time, file_time
        access_time = file_time

        FileTimeToLocalFileTime write_time, file_time
        write_time = file_time
    End If

    ' Convert into dates.
    date_created = FileTimeToDate(creation_time)
    date_accessed = FileTimeToDate(access_time)
    date_written = FileTimeToDate(write_time)
End Function

' Convert the FILETIME structure into a Date.
Private Function FileTimeToDate(ft As FILETIME) As Date
' FILETIME units are 100s of nanoseconds.
Const TICKS_PER_SECOND = 10000000

Dim lo_time As Double
Dim hi_time As Double
Dim seconds As Double
Dim hours As Double
Dim the_date As Date

    ' Get the low order data.
    If ft.dwLowDateTime < 0 Then
        lo_time = 2 ^ 31 + (ft.dwLowDateTime And &H7FFFFFFF)
    Else
        lo_time = ft.dwLowDateTime
    End If

    ' Get the high order data.
    If ft.dwHighDateTime < 0 Then
        hi_time = 2 ^ 31 + (ft.dwHighDateTime And _
            &H7FFFFFFF)
    Else
        hi_time = ft.dwHighDateTime
    End If

    ' Combine them and turn the result into hours.
    seconds = (lo_time + 2 ^ 32 * hi_time) / _
        TICKS_PER_SECOND
    hours = CLng(seconds / 3600)
    seconds = seconds - hours * 3600

    ' Make the date.
    the_date = DateAdd("h", hours, "1/1/1601 0:00 AM")
    the_date = DateAdd("s", seconds, the_date)
    FileTimeToDate = the_date
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated