Změna datumu poslední modifikace souboru

Postup:
V deklarační části zapište:
Private Type FILETIME
   dwLowDate As Long
   dwHighDate 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
   wMillisecs As Integer
End Type

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

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 LocalFileTimeToFileTime Lib _
   "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long

Private Declare Function SetFileTime Lib "kernel32" _
   (ByVal hFile As Long, ByVal MullP As Long, ByVal NullP2 As Long, _
   lpLastWriteTime As FILETIME) As Long

Private Declare Function SystemTimeToFileTime Lib _
   "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long

Private Declare Function CloseHandle Lib "kernel32" _
   (ByVal hObject As Long) As Long

Funkce:

Public Function SetFileDateTime(ByVal FileName As String, _
   ByVal TheDate As String) As Boolean

   'PARAMETRY: TheDate --Datum pro nastavení
   'FileName -- Jméno souboru
   'NÁVRATOVÉ HODNOTY: True při úspěchu, jinak False

   If Dir(FileName) = "" Then Exit Function
   If Not IsDate(TheDate) Then Exit Function

   Dim lFileHnd As Long
   Dim lRet As Long

   Dim typFileTime As FILETIME
   Dim typLocalTime As FILETIME
   Dim typSystemTime As SYSTEMTIME

   With typSystemTime
      .wYear = Year(TheDate)
      .wMonth = Month(TheDate)
      .wDay = Day(TheDate)
      .wDayOfWeek = Weekday(TheDate) - 1
      .wHour = Hour(TheDate)
      .wMinute = Minute(TheDate)
      .wSecond = Second(TheDate)
   End With

   lRet = SystemTimeToFileTime(typSystemTime, typLocalTime)
   lRet = LocalFileTimeToFileTime(typLocalTime, typFileTime)

   lFileHnd = CreateFile(FileName, GENERIC_WRITE, _
      FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, _
      OPEN_EXISTING, 0, 0)

   lRet = SetFileTime(lFileHnd, ByVal 0&, ByVal 0&, typFileTime)

   CloseHandle lFileHnd

   SetFileDateTime = lRet > 0

End Function

Zpět

Autor: The Bozena