vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#

https://www.vbarchiv.net
Rubrik: Dateisystem · Dateien allgemein   |   VB-Versionen: VB4, VB5, VB627.11.00
Datum und Uhrzeit einer Datei ändern

Für das Ändern der Zeitangaben (Datum/Uhrzeit) einer Datei bietet Visual-Basic keine entsprechenden Befehle. Mit Hilfe der Windows-API ist es jedoch...

Autor:   Dieter OtterBewertung:  Views:  34.560 
www.tools4vb.deSystem:  Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 Beispielprojekt auf CD 

Für das Ändern der Zeitangaben (Datum/Uhrzeit) einer Datei bietet Visual-Basic keine entsprechenden Befehle. Mit Hilfe der Windows-API ist es jedoch möglich die Zeitangaben einer Datei zu ändern. Hierbei können folgende Angaben neu gesetzt werden:

  • Datum/Zeit der Erstellung
  • Datum/Zeit des letzten Zugriffes
  • Datum/Zeit der letzten Änderung
Hier der Quellcode:

' zunächst die benötigten Deklarationen
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
 
' Die beiden nachfolgenden Routinen werden zum 
Lesen/Schreiben der Zeitangaben benötigt
Private Function ReadFileTime(ByVal sFilename As String, _
  tCreation As Date, tLastAccess As Date, _
  tLastWrite As Date) As Boolean
  ' 
  ' *** Datum/Zeitwert einer Datei ermitteln
  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(sFilename, 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$(.wDay) & "." & _
          Format$(.wMonth) & "." & Format$(.wYear) & " " & _
          Format$(.wHour) & ":" & Format$(.wMinute, "00") & _
          ":" & Format$(.wSecond, "00"))
      End With
 
      ' Letzter Zugriff
      FileTimeToLocalFileTime ftLastAccess, LocalFileTime
      FileTimeToSystemTime LocalFileTime, LocalSystemTime
      With LocalSystemTime
        tLastAccess = CDate(Format$(.wDay) & "." & _
          Format$(.wMonth) & "." & Format$(.wYear) & " " & _
          Format$(.wHour) & ":" & Format$(.wMinute, "00") & _
          ":" & Format$(.wSecond, "00"))
      End With
 
      ' Letzte Änderung
      FileTimeToLocalFileTime ftLastWrite, LocalFileTime
      FileTimeToSystemTime LocalFileTime, LocalSystemTime
      With LocalSystemTime
        tLastWrite = CDate(Format$(.wDay) & "." & _
          Format$(.wMonth) & "." & Format$(.wYear) & " " & _
          Format$(.wHour) & ":" & Format$(.wMinute, "00") & _
          ":" & Format$(.wSecond, "00"))
      End With
 
      ReadFileTime = True
    End If
    CloseHandle fHandle
  End If
End Function
 
Private Function WriteFileTime(ByVal sFilename As String, _
  ByVal tCreation As Date, ByVal tLastAccess As Date, _
  ByVal tLastWrite As Date) As Boolean
  ' 
  ' *** Datum/Zeitwert einer Datei setzen
  Dim fHandle As Long
 
  Dim ftCreation As FileTime
  Dim ftLastAccess As FileTime
  Dim ftLastWrite As FileTime
  Dim LocalFileTime As FileTime
  Dim LocalSystemTime As SYSTEMTIME
 
  WriteFileTime = False
  fHandle = CreateFile(sFilename, GENERIC_WRITE, 0, _
    0, OPEN_EXISTING, 0, 0)
  If fHandle <> -1 Then
    ' Erstellungsdatum
    With LocalSystemTime
      .wDay = Day(tCreation)
      .wDayOfWeek = Weekday(tCreation)
      .wMonth = Month(tCreation)
      .wYear = Year(tCreation)
      .wHour = Hour(tCreation)
      .wMinute = Minute(tCreation)
      .wSecond = Second(tCreation)
    End With
    SystemTimeToFileTime LocalSystemTime, LocalFileTime
    LocalFileTimeToFileTime LocalFileTime, ftCreation
 
    ' Letzter Zugriff
    With LocalSystemTime
      .wDay = Day(tLastAccess)
      .wDayOfWeek = Weekday(tLastAccess)
      .wMonth = Month(tLastAccess)
      .wYear = Year(tLastAccess)
      .wHour = Hour(tLastAccess)
      .wMinute = Minute(tLastAccess)
      .wSecond = Second(tLastAccess)
    End With
    SystemTimeToFileTime LocalSystemTime, LocalFileTime
    LocalFileTimeToFileTime LocalFileTime, ftLastAccess
 
    ' Letzte Änderung
    With LocalSystemTime
      .wDay = Day(tLastWrite)
      .wDayOfWeek = Weekday(tLastWrite)
      .wMonth = Month(tLastWrite)
      .wYear = Year(tLastWrite)
      .wHour = Hour(tLastWrite)
      .wMinute = Minute(tLastWrite)
      .wSecond = Second(tLastWrite)
    End With
    SystemTimeToFileTime LocalSystemTime, LocalFileTime
    LocalFileTimeToFileTime LocalFileTime, ftLastWrite
 
    If SetFileTime(fHandle, ftCreation, ftLastAccess, _
      ftLastWrite) <> 0 Then
      WriteFileTime = True
    End If
    CloseHandle fHandle
  End If
End Function

Aufruf der Funktionen:

Dim tCreation As Date	' Erstellt am
Dim tLastAccess As Date	' Letzter Zugriff
Dim tLastWrite As Date	' Letzte Änderung
 
' Zeitangaben lesen
If ReadFileTime(Datei, tCreation, tLastAccess, tLastWrite) Then
  ' Erstellungsdatum ändern
  tCreation = CDate("01.11.2000 17:35:41")
 
  ' Datum "Letzter Zugriff" ändern
  tLastAccess = CDate("01.11.2000 17:35:41")
 
  ' Datum "Letzter Änderung" ändern
  tLastWrite = CDate("01.11.2000 17:35:41")
 
  ' Zeitangaben setzen
  WriteFileTime Datei, tCreation, tLastAccess, tLastWrite
End If



Anzeige

Kauftipp Unser Dauerbrenner!Diesen und auch alle anderen Tipps & Tricks finden Sie auch auf unserer aktuellen vb@rchiv  Vol.6
(einschl. Beispielprojekt!)

Ein absolutes Muss - Geballtes Wissen aus mehr als 8 Jahren vb@rchiv!
- nahezu alle Tipps & Tricks und Workshops mit Beispielprojekten
- Symbol-Galerie mit mehr als 3.200 Icons im modernen Look
Weitere Infos - 4 Entwickler-Vollversionen (u.a. sevFTP für .NET), Online-Update-Funktion u.v.m.
 
 
Copyright ©2000-2024 vb@rchiv Dieter OtterAlle Rechte vorbehalten.


Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.