Rubrik: Datum/Zeit und Timer · Datums- und Zeitfunktionen | VB-Versionen: VB4, VB5, VB6 | 03.08.01 |
Alle Datumsangaben einer Datei ermitteln Eine Funktion, welche das Erstellungsdatum, das Datum des letzten Zugriffs und der letzten Speicherung einer Datei zurückgibt. | ||
Autor: Dieter Otter | Bewertung: | Views: 47.107 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Über die VB-Funktion FileDateTime lässt sich bekanntlich immer nur das Datum des letzten Schreibvorgangs einer Datei ermitteln. Zu einer Datei werden vom System aber noch das Erstellungsdatum und das Datum des letzten Dateizugriffs gespeichert.
Wie kann man nun alle diese Datumsangaben in Erfahrung bringen?
Nun ja, wie fast immer, wenn die VB eigenen Mittel "versagen", über das Windows-API!
Im Nachfolgenden möchten wir Ihnen eine universelle Funktion vorstellen, welche alle drei oben genannten Datumsangaben einer Datei ermittelt und als Date-Variablen zurückgibt.
Den nachfolgenden Code in ein Modul einfügen:
' zunächst die benötigten API-Deklarationen ' Datei Datum/Zeit Private Type FileTime dwLowDateTime As Long dwHighDateTime As Long 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 Declare Function CreateFile Lib "kernel32" _ Alias "CreateFileA" ( _ ByVal lpFilename As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ ByVal lpSecurityAttributes As Long, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long Private Declare Function CloseHandle Lib "kernel32" ( _ ByVal hObject As Long) 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 SetFileTime Lib "kernel32" ( _ ByVal hFile As Long, _ lpCreationTime As FileTime, _ lpLastAccessTime As FileTime, _ lpLastWriteTime As FileTime) As Long Private Declare Function FileTimeToLocalFileTime Lib "kernel32" ( _ lpFileTime As FileTime, _ lpLocalFileTime As FileTime) As Long Private Declare Function FileTimeToSystemTime Lib "kernel32" ( _ lpFileTime As FileTime, _ lpSystemTime As SYSTEMTIME) As Long Private Declare Function SystemTimeToFileTime Lib "kernel32" ( _ lpSystemTime As SYSTEMTIME, _ lpFileTime As FileTime) As Long Private Declare Function LocalFileTimeToFileTime Lib "kernel32" ( _ lpLocalFileTime As FileTime, _ lpFileTime As FileTime) As Long Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const OPEN_EXISTING = 3 ' Datum/Zeit einer Datei ermitteln Public Function ReadFileTime(ByVal lpFilename As String, _ tCreation As Date, tLastAccess As Date, _ tLastWrite As Date) As Boolean Dim fHandle As Long Dim ftCreation As FileTime Dim ftLastAccess As FileTime Dim ftLastWrite As FileTime Dim LocalFileTime As FileTime Dim LocalSystemTime As SYSTEMTIME ReadFileTime = False fHandle = CreateFile(lpFilename, GENERIC_READ, 0, _ 0, OPEN_EXISTING, 0, 0) If fHandle <> -1 Then ' Zeitinformationen auslesen If GetFileTime(fHandle, ftCreation, ftLastAccess, _ ftLastWrite) <> 0 Then ' Erstellungsdatum FileTimeToLocalFileTime ftCreation, LocalFileTime FileTimeToSystemTime LocalFileTime, LocalSystemTime With LocalSystemTime tCreation = CDate(Format$(DateSerial(.wYear, _ .wMonth, .wDay), "Short Date") & " " & _ Format$(.wHour) & ":" & _ Format$(.wMinute, "00") & ":" & _ Format$(.wSecond, "00")) End With ' Letzter Zugriff FileTimeToLocalFileTime ftLastAccess, LocalFileTime FileTimeToSystemTime LocalFileTime, LocalSystemTime With LocalSystemTime tLastAccess = CDate(Format$(DateSerial(.wYear, _ .wMonth, .wDay), "Short Date") & " " & _ Format$(.wHour) & ":" & _ Format$(.wMinute, "00") & ":" & _ Format$(.wSecond, "00")) End With ' Letzte Änderung FileTimeToLocalFileTime ftLastWrite, LocalFileTime FileTimeToSystemTime LocalFileTime, LocalSystemTime With LocalSystemTime tLastWrite = CDate(Format$(DateSerial(.wYear, _ .wMonth, .wDay), "Short Date") & " " & _ Format$(.wHour) & ":" & _ Format$(.wMinute, "00") & ":" & _ Format$(.wSecond, "00")) End With ReadFileTime = True End If CloseHandle fHandle End If End Function
Beispiel:
Dim tCreation As Date Dim tLastAccess As Date Dim tLastWrite As Date Dim sFilename As String sFilename = "c:\eigene dateien\MeineDatei.txt" ReadFileTime sFilename, tCreation, _ tLastAccess, tLastWrite MsgBox "Erstellungsdatum: " & _ Format$(tCreation, "dd.mm.yyyy hh:mm:ss") & _ vbCrLf & "Letzter Zugriff am: " & _ Format$(tLastAccess, "dd.mm.yyyy hh:mm:ss") & _ vbCrLf & "Letzter Schreibvorgang: " & _ Format$(tLastWrite, "dd.mm.yyyy hh:mm:ss")