Zurück zur Homepage

Dateiattribute ändern

Erstellungszeitpunkt, Letzter Zugriff und Letzte Änderung lassen sich modifizieren.Ohne FSO! Das ist hilfreich, wenn man verschleiern will, wann die letzte Modifikation einer Datei durchgeführt wurde. ;-)

Beispieldatei (Fileinfos.zip 29 kB)

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 SetFileTime Lib "kernel32" _
  (ByVal hFile As Long, lpCreationTime As FILETIME, _
  lpLastAccessTime As FILETIME, lpLastWriteTime As _
  FILETIME) 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 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 Type OPENFILENAME
  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As Long
  lpstrFileTitle As String
  nMaxFileTitle As Long
  lpstrInitialDir As String
  lpstrTitle As String
  flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As String
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type

Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_WRITE = &H2

Public Sub FileZeitÄndern(Filename As String, Erstellt As Date, _
  Geändert As Date, LetzterZugriff As Date)
Dim hwndFile As Long
Dim udtCreationFileTime As FILETIME
Dim udtLastAccessFileTime As FILETIME
Dim udtLastWriteFileTime As FILETIME
Dim strFilename As String
  'Dateiname holen
  strFilename = Filename
  If strFilename = "" Then Exit Sub
  'Erstellungszeitpunkt
  udtCreationFileTime = ToFileTime(Erstellt)
  'Letzter Zugriff
  udtLastAccessFileTime = ToFileTime(LetzterZugriff)
  'Letzte Änderung
  udtLastWriteFileTime = ToFileTime(Geändert)
  'Filehandle holen
  hwndFile = CreateFile(strFilename, GENERIC_WRITE, _
    FILE_SHARE_WRITE, ByVal 0&, _
    OPEN_EXISTING, 0&, 0&)
  'Filezeiten ändern
  SetFileTime hwndFile, udtCreationFileTime, _
    udtLastAccessFileTime, udtLastWriteFileTime
  'Filehandle schließen
  CloseHandle hwndFile
End Sub

Private Function ToFileTime(Zeitpunkt As Date) _
  As FILETIME
Dim udtSystemzeit As SYSTEMTIME
  Zeitpunkt = GmtToNormal(Zeitpunkt)
  With udtSystemzeit
    .wYear = Year(Zeitpunkt)
    .wMonth = Month(Zeitpunkt)
    .wDay = Day(Zeitpunkt)
    .wDayOfWeek = WeekDay(Zeitpunkt) - 1
    .wHour = Hour(Zeitpunkt)
    .wSecond = Second(Zeitpunkt)
  End With
  'Umwandlung Systemzeit zu Filezeit. Es wird aber
  'der Offset von UTC zur Ortszeit aufgeschlagen
  'deshalb GmtToNormal im Modul mdlBenutzerdefiniert
  SystemTimeToFileTime udtSystemzeit, ToFileTime
  'Umwandlung Ortszeit zu UTC. Hier wird
  'der Offset von UTC zur Ortszeit abgezogen
  LocalFileTimeToFileTime ToFileTime, ToFileTime
End Function

Public Function NormalToGMT(DatumZeit As Date) As Date
Dim Beginn As Date, Ende As Date, Jahr&
  Jahr = Year(DatumZeit)
  Beginn = DateSerial(Jahr, 4, 0) - (WeekDay( _
    DateSerial(Jahr, 4, 0), 2) Mod 7) + TimeSerial(2, 0, 0)
  Ende = DateSerial(Jahr, 11, 0) - (WeekDay( _
    DateSerial(Jahr, 11, 0), 2) Mod 7) + TimeSerial(2, 0, 0)
  If DatumZeit > Beginn And DatumZeit < Ende Then
    NormalToGMT = DatumZeit - TimeSerial(2, 0, 0)
  Else
    NormalToGMT = DatumZeit - TimeSerial(1, 0, 0)
  End If
End Function

Public Function GmtToNormal(DatumZeit As Date) As Date
Dim Beginn As Date, Ende As Date, Jahr&
  Jahr = Year(DatumZeit)
  Beginn = DateSerial(Jahr, 4, 0) - (WeekDay( _
  DateSerial(Jahr, 4, 0), 2) Mod 7) + TimeSerial(1, 0, 0)
  Ende = DateSerial(Jahr, 11, 0) - (WeekDay( _
  DateSerial(Jahr, 11, 0), 2) Mod 7) + TimeSerial(1, 0, 0)
  If DatumZeit > Beginn And DatumZeit < Ende Then
    GmtToNormal = DatumZeit + TimeSerial(2, 0, 0)
  Else
    GmtToNormal = DatumZeit + TimeSerial(1, 0, 0)
  End If
End Function