Article ID: 154821
Article Last Modified on 7/1/2004
Option Explicit
Private Const OF_READ = &H0
Private Const OF_SHARE_DENY_NONE = &H40
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCTREC
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Type FILETIMEREC
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SYSTEMTIMEREC
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FILETIMEREC, lpSystemTime As SYSTEMTIMEREC) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal _
hFile As Long, lpCreationTime As FILETIMEREC, lpLastAccessTime _
As FILETIMEREC, lpLastWriteTime As FILETIMEREC) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As _
String, lpReOpenBuff As OFSTRUCTREC, ByVal wStyle As Long) As Long
Private Declare Function hread Lib "kernel32" Alias "_hread" _
(ByVal hFile As Long, lpBuffer As Any, ByVal lBytes As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal _
hFile As Long) As Long
Sub Form_Load()
Command1.Caption = "&Get last write time"
Text1.Text = "C:\AUTOEXEC.BAT"
End Sub
Private Sub Command1_Click()
Dim sInpFile As String
Dim hFile As Integer
Dim FileStruct As OFSTRUCTREC
Dim iRC As Integer
Dim CreationTime As FILETIMEREC
Dim LastAccessTime As FILETIMEREC
Dim LastWriteTime As FILETIMEREC
Dim SystemTime As SYSTEMTIMEREC
sInpFile = Trim(Text1.Text)
' Check that the file exists.
If Len(Dir(sInpFile)) = 0 Then
MsgBox "Can't find the file", vbExclamation
Exit Sub
End If
' Open it to get a stream handle.
hFile = OpenFile(sInpFile, FileStruct, OF_READ Or OF_SHARE_DENY_NONE)
If hFile = 0 Then
MsgBox "Can't open the file", vbExclamation
Exit Sub
End If
If GetFileTime(hFile, CreationTime, _
LastAccessTime, LastWriteTime) Then
' Massage time into format that we can use.
If Not FileTimeToSystemTime(LastWriteTime, SystemTime) Then
Print "Year of file :" & SystemTime.wYear
Print "Month of File :" & SystemTime.wMonth
Print "Day of File :" & SystemTime.wDay
Else
MsgBox "FileTimeToSystemTime Failed"
End If
Else
MsgBox "GetFileTime Failed"
End If
iRC = lclose(hFile)
End Sub
Keywords: kbhowto kbapi KB154821