VB-Tec.de Visual Basic - Technik, FAQ, Tricks, BeispieleDatei-Zeiten abfragen |
Dim Pfad As String Pfad = "C:\Daten\Texte\Test.txt" 'Bekannte VB-Funktion: MsgBox "Geändert: " & FileDateTime(Pfad) 'Neue Funktionen (via API): MsgBox "Angelegt: " & FileCreationTime(Pfad) MsgBox "Zugriff: " & FileLastAccessTime(Pfad)
Private Declare Sub FileTimeToLocalFileTime Lib "kernel32" ( _
lpFileTime As FILETIME, lpLocalFileTime As FILETIME)
Private Declare Sub FileTimeToSystemTime Lib "kernel32" ( _
lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME)
Private Declare Sub FindClose Lib "kernel32" ( _
ByVal hFindFile As Long)
Private Declare Function FindFirstFileA Lib "kernel32" ( _
ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA _
) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type
Private Type SYSTEMTIME
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 Const INVALID_VALUE = -1
Private Const vbErr_FileNotFound = 53
Die Funktion FileCreationTime bestimmt den Zeitpunkt, an dem eine Datei angelegt/erzeugt worden ist:
Public Function FileCreationTime( _
ByRef Path As String _
) As Variant
Dim hFind As Long
Dim WFD As WIN32_FIND_DATA
hFind = FindFirstFileA(Path, WFD)
If hFind = INVALID_VALUE Then
Err.Raise vbErr_FileNotFound
Else
FileCreationTime = CSysTime(WFD.ftCreationTime)
FindClose hFind
End If
End Function
Die Funktion FileLastAccessTime gibt an, wann eine Datei zuletzt geöffnet bzw. darauf zugegriffen worden ist:
Public Function FileLastAccessTime( _
ByRef Path As String _
) As Variant
Dim hFind As Long
Dim WFD As WIN32_FIND_DATA
hFind = FindFirstFileA(Path, WFD)
If hFind = INVALID_VALUE Then
Err.Raise vbErr_FileNotFound
Else
FileLastAccessTime = CSysTime(WFD.ftLastAccessTime)
FindClose hFind
End If
End Function
Beide Funktionen benötigen eine Umrechnung der Betriebssystem-internen Zeit-Darstellung in die VB-Welt:
Private Function CSysTime( _
ByRef FT As FILETIME _
) As Variant
Dim ST As SYSTEMTIME
FileTimeToLocalFileTime FT, FT
FileTimeToSystemTime FT, ST
With ST
CSysTime = _
DateSerial(.wYear, .wMonth, .wDay) + _
TimeSerial(.wHour, .wMinute, .wSecond)
End With
End Function© Jost Schwider, 23.12.2000-23.12.2000 - http://vb-tec.de/filetime.htm