ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 利用API修改文件创建时间、最后修改时间、最后访问时间

利用API修改文件创建时间、最后修改时间、最后访问时间

作者:绿色风 分类: 时间:2022-08-18 浏览:108
楼主
amulee
与获取时间相反的过程,当然使用的API也有所不同。但是原理非常简单。
  1. Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
  2. Private Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
  3. Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
  4. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  5. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  6. '文件结构
  7. Type FILETIME
  8.     dwLowDateTime As Long
  9.     dwHighDateTime As Long
  10. End Type
  11. Type SECURITY_ATTRIBUTES
  12.     nLength As Long
  13.     lpSecurityDescriptor As Long
  14.     bInheritHandle As Long
  15. End Type
  16. Type WIN32_FIND_DATA
  17.     dwFileAttributes As Long
  18.     ftCreationTime As FILETIME
  19.     ftLastAccessTime As FILETIME
  20.     ftLastWriteTime As FILETIME
  21.     nFileSizeHigh As Long
  22.     nFileSizeLow As Long
  23.     dwReserved0 As Long
  24.     dwReserved1 As Long
  25.     cFileName As String * 255
  26.     cAlternate As String * 14
  27. End Type
  28. Private Type SYSTEMTIME
  29.     wYear As Integer
  30.     wMonth As Integer
  31.     wDayOfWeek As Integer
  32.     wDay As Integer
  33.     wHour As Integer
  34.     wMinute As Integer
  35.     wSecond As Integer
  36.     wMilliseconds As Integer
  37. End Type
  38. Private Const GENERIC_WRITE = &H40000000
  39. Private Const OPEN_EXISTING = 3
  40. Private Const FILE_SHARE_READ = &H1
  41. Private Const FILE_SHARE_WRITE = &H2
  42. Sub Test()
  43.     Dim FindData As WIN32_FIND_DATA
  44.     Dim CTime As FILETIME, LATime As FILETIME, LWTime As FILETIME
  45.     Dim CTime1 As FILETIME, LATime1 As FILETIME, LWTime1 As FILETIME
  46.     Dim CTime2 As SYSTEMTIME, LATime2 As SYSTEMTIME, LWTime2 As SYSTEMTIME
  47.     Dim SA As SECURITY_ATTRIBUTES
  48.     Dim FileHandle As Long
  49.     Dim SetHandle As Long
  50.     Dim TimeHandle As Long
  51.     Dim FPath As String
  52.     Dim StrA As String
  53.     Dim dtNew As Date
  54.     FPath = ThisWorkbook.Path & "\1.txt"
  55.     dtNew = Now
  56.     '设定创建时间
  57.     CTime2.wYear = Year(dtNew)
  58.     CTime2.wMonth = Month(dtNew)
  59.     CTime2.wDay = Day(dtNew)
  60.     CTime2.wHour = Hour(dtNew)
  61.     CTime2.wMinute = Minute(dtNew)
  62.     CTime2.wSecond = Second(NdtNew)
  63.     '设定最后访问
  64.     LATime2.wYear = Year(dtNew)
  65.     LATime2.wMonth = Month(dtNew)
  66.     LATime2.wDay = Day(dtNew)
  67.     LATime2.wHour = Hour(dtNew)
  68.     LATime2.wMinute = Minute(dtNew)
  69.     LATime2.wSecond = Second(NdtNew)
  70.     '设定最后修改
  71.     LWTime2.wYear = Year(dtNew)
  72.     LWTime2.wMonth = Month(dtNew)
  73.     LWTime2.wDay = Day(dtNew)
  74.     LWTime2.wHour = Hour(dtNew)
  75.     LWTime2.wMinute = Minute(dtNew)
  76.     LWTime2.wSecond = Second(NdtNew)
  77.     '时间转换为文件时间
  78.     SystemTimeToFileTime CTime2, CTime1
  79.     SystemTimeToFileTime LATime2, LATime1
  80.     SystemTimeToFileTime LWTime2, LWTime1
  81.     '本地时间转换为标准时间
  82.     LocalFileTimeToFileTime CTime1, CTime
  83.     LocalFileTimeToFileTime LATime1, LATime
  84.     LocalFileTimeToFileTime LWTime1, LWTime
  85.     '打开文件,获取文件句柄
  86.     FileHandle = CreateFile(FPath, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0)
  87.     '写入时间
  88.     SetHandle = SetFileTime(FileHandle, CTime, LATime, LWTime)
  89.     '关闭文件
  90.     CloseHandle FileHandle
  91.     If SetHandle <> 0 Then MsgBox "修改成功" Else MsgBox "修改失败"
  92. End Sub


如果要批量只需要在下面语句两端加上循环修改不同的FPath即可

  1. '打开文件,获取文件句柄
  2. FileHandle = CreateFile(FPath, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0)
  3. '写入时间
  4. SetHandle = SetFileTime(FileHandle, CTime, LATime, LWTime)
  5. '关闭文件
  6. CloseHandle FileHandle


附件参考:
API修改文件创建时间、最后修改时间、最后访问时间.rar
2楼
opelwang
感谢版主指点,此问题的解决方法我找了好久了。
3楼
kangguowei
vba 真强大.

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap