Изменить дату и время создания файла - VBA

Узнай цену своей работы

Формулировка задачи:

Можно ли средствами VBA изменить дату создания текстового файла?

Решение задачи: «Изменить дату и время создания файла»

textual
Листинг программы
  1. Option Explicit
  2. Public Const GENERIC_WRITE = &H40000000, GENERIC_READ = &H80000000, FILE_ATTRIBUTE_NORMAL = &H80, OPEN_EXISTING = 3
  3. Public Type FileTime:  dwLowDateTime As Long:  dwHighDateTime As Long: End Type
  4. Public 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
  5. 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
  6. Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FileTime, lpLastAccessTime As FileTime, lpLastWriteTime As FileTime) As Long
  7. Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FileTime) As Long
  8. Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpFileTime As FileTime, lpLocalFileTime As FileTime) As Long
  9. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  10.  
  11. Function DTtoFT(ByVal DT As Date) As FileTime
  12. Dim ST As SYSTEMTIME, lTime As FileTime
  13. ST.wYear = Year(DT): ST.wMonth = Month(DT): ST.wDay = Day(DT): ST.wHour = Hour(DT): ST.wMinute = Minute(DT): ST.wSecond = Second(DT)
  14. SystemTimeToFileTime ST, lTime
  15. LocalFileTimeToFileTime lTime, DTtoFT
  16. End Function
  17.  
  18. Sub GetFTime(fName As String, Creation As Date, LastAccess As Date, LastWrite As Date)
  19. Dim hFile As Long, ct As FileTime, at As FileTime, wt As FileTime
  20. hFile = CreateFile(fName, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  21. GetFileTime hFile, ct, at, wt
  22. CloseHandle hFile
  23. Creation = FTtoDT(ct): LastAccess = FTtoDT(at): LastWrite = FTtoDT(wt)
  24. End Sub
  25.  
  26. Sub SetFTime(fName As String, Optional Creation As Date = -657434, Optional LastAccess As Date = -657434, Optional LastWrite As Date = -657434)
  27. Dim hFile As Long, ct As Date, at As Date, wt As Date
  28. If Creation = -657434 Or LastAccess = -657434 Or LastWrite = -657434 Then
  29.   GetFTime fName, ct, at, wt
  30.   If Creation = -657434 Then Creation = ct
  31.   If LastAccess = -657434 Then LastAccess = at
  32.   If LastWrite = -657434 Then LastWrite = wt
  33. End If
  34. hFile = CreateFile(fName, GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  35. SetFileTime hFile, DTtoFT(Creation), DTtoFT(LastAccess), DTtoFT(LastWrite)
  36. CloseHandle hFile
  37. End Sub
  38.  
  39. Sub SetFT()
  40. Dim T1 As Date, T2 As Date, T3 As Date
  41. T1 = Now 'Creation As Date - сейчас
  42. T2 = T1 + 2 'LastAccess As Date - послезавтра в это же время
  43. T3 = T1 + 1 'LastWrite As Date - завтра в это же время
  44. 'собственно примерчик
  45. SetFTime "d:\tmp\123.txt", T1, T2, T3
  46. '
  47. End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.733 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут