Forums Neueste Beiträge
 

Datei-Zeitstempel auslesen funktioniert nicht bei geöffneten Dateien

02/04/2012 - 14:12 von Ahmed Martens | Report spam
Hallo Leute,

mit folgender Funktion (benötigt weitere API's) kann ich den
Datei-Zeitstempel von Dateien auslesen:

<Code>

' 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 <> 0 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

</Code>

Das funktioniert aber nur, wenn die Datei nicht anderweitig verwendet
wird. Kann ich auch irgendwie den Zeitstempel von geöffneten Dateien
auslesen?

Vielen Dank im voraus.

Gruß Ahmed
Antworten bitte nur in der Newsgroup.
Win7 Prof. 64bit / MS-Office 2010 Prof. 32bit
 

Lesen sie die antworten

#1 Ahmed Martens
02/04/2012 - 15:22 | Warnen spam
Hallo Leute,

ich habe mein Problem einfach über eine weitere Hilfsfunktion lösen
können:

<Code>
' 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 <> 0 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
Else
'Falls kein fHandle erstellt werden konnte, wird einfach
VBSript verwendet
ReadFileTime = ReadFileTime_VBS(lpFileName, tCreation,
tLastAccess, _
tLastWrite)
End If
CloseHandle fHandle
End If
End Function

'Hilfsfunktion (wird nur verwendet, wenn Datei benutzt wird)
Private Function ReadFileTime_VBS(ByVal lpFileName As String, tCreation
As Date, tLastAccess As Date, tLastWrite As Date) As Boolean
Dim FSO As Object
Dim F As Object
10 On Error GoTo ReadFileTime_WSH_Error

20 Set FSO = CreateObject("Scripting.FileSystemObject")
30 Set F = FSO.GetFile(lpFileName)

40 tCreation = F.DateCreated
50 tLastAccess = F.DateLastAccessed
60 tLastWrite = F.DateLastModified

70 ReadFileTime_VBS = True


80 On Error GoTo 0
90 Exit Function

ReadFileTime_WSH_Error:

100 MsgBox "Fehlernr.: " & Err.Number & " (" & Err.Description &
") in Prozedur ReadFileTime_WSH von Modul mdlDateiauswahl", , "Fehler in
Zeile: " & Erl

End Function

</Code>

Gruß Ahmed
Antworten bitte nur in der Newsgroup.
Win7 Prof. 64bit / MS-Office 2010 Prof. 32bit

Ähnliche fragen