VB and VBA Users Source Code: Returning a files creation time
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning a files creation time
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, July 17, 2001
Hits:
1701
Category:
Windows API
Article:
The following function returns the creation time of a file: Option Explicit Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Const MaxPathName = 256 Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(MaxPathName) As Byte End Type Private Type FILETIME LowDateTime As Long HighDateTime As Long End Type Private Type SYSTEMTIME Year As Integer Month As Integer DayOfWeek As Integer Day As Integer Hour As Integer Minute As Integer Second As Integer Milliseconds As Integer End Type 'Purpose : Returns the file creation time for a specified file. 'Inputs : sPathFileName The path and file name of the file. 'Outputs : Returns the file creation time of the specified file, ' or returns -1 (29/12/1899) if an error occurred. 'Author : Andrew Baker 'Date : 13/Jul/2001 'Notes : Function FileCreationTime(sPathFileName As String) As Date Dim lHFile As Long, lRetVal As Long Dim tCreationTime As FILETIME, tLassAccessTime As FILETIME, tOFS As OFSTRUCT Dim tLastWriteTime As FILETIME, tSysTime As SYSTEMTIME, tCreationTimeLocal As FILETIME Const clOfRead = &H0 On Error Resume Next tOFS.cBytes = Len(tOFS) lHFile = OpenFile(sPathFileName, tOFS, clOfRead) If lHFile Then lRetVal = GetFileTime(lHFile, tCreationTime, tLassAccessTime, tLastWriteTime) lRetVal = FileTimeToLocalFileTime(tCreationTime, tCreationTimeLocal) lRetVal = FileTimeToSystemTime(tCreationTimeLocal, tSysTime) FileCreationTime = DateSerial(tSysTime.Year, tSysTime.Month, tSysTime.Day) + TimeSerial(tSysTime.Hour, tSysTime.Minute, tSysTime.Second) lRetVal = CloseHandle(lHFile) End If If Err.LastDllError Then 'An error occurred (check if file is missing) Debug.Print "Error in FileCreationTime. Error " & Err.LastDllError & " (check the file exists)." FileCreationTime = -1 End If On Error GoTo 0 End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder