ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何用vba代码修改文件夹创建时间?

如何用vba代码修改文件夹创建时间?

作者:绿色风 分类: 时间:2022-08-17 浏览:182
楼主
kevinchengcw
Q: 如何用vba代码修改文件夹创建时间?
A: 代码如下:(下述代码不仅修改创建时间,还同时修改了最后访问时间及最后修改时间)
  1. '声明需调用的API函数
  2. Public 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
  3. Public Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
  4. Public Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
  5. Public Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
  6. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

  7. '定义需引用的数据类型
  8. Public Type FILETIME
  9.     dwLowDateTime As Long
  10.     dwHighDateTime As Long
  11. End Type
  12. Public Type SECURITY_ATTRIBUTES
  13.     nLength As Long
  14.     lpSecurityDescriptor As Long
  15.     bInheritHandle As Long
  16. End Type
  17. Public Type SYSTEMTIME
  18.     wYear As Integer
  19.     wMonth As Integer
  20.     wDayOfWeek As Integer
  21.     wDay As Integer
  22.     wHour As Integer
  23.     wMinute As Integer
  24.     wSecond As Integer
  25.     wMilliseconds As Integer
  26. End Type

  27. '定义要使用的常量
  28. Public Const GENERIC_READ = &H80000000
  29. Public Const GENERIC_WRITE = &H40000000
  30. Public Const FILE_SHARE_READ = &H1
  31. Public Const FILE_SHARE_WRITE = &H2
  32. Public Const OPEN_EXISTING = 3
  33. Public Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
  34. Public Const INVALID_HANDLE_value = -1

  35. '定义修改文件夹时间函数
  36. Public Function SetDirTime(DirName As String, NewTime As SYSTEMTIME) As Boolean
  37. Dim hDir As Long
  38. Dim lpCreationTime As FILETIME
  39. Dim lpLastAccessTime As FILETIME
  40. Dim lpLastWriteTime As FILETIME
  41. Dim lptLocalTime As FILETIME
  42. Dim RetVal As Boolean
  43. Dim sAttribute As SECURITY_ATTRIBUTES
  44. hDir = CreateFile(DirName, GENERIC_WRITE, FILE_SHARE_READ, sAttribute, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)  '打开目录获取句柄
  45. If hDir = INVALID_HANDLE_value Then  '如果句柄无效返回False,退出函数
  46.     SetDirTime = False
  47.     Exit Function
  48. End If
  49. Call SystemTimeToFileTime(NewTime, lptLocalTime)  '将输入时间转换为文件时间格式
  50. Call LocalFileTimeToFileTime(lptLocalTime, lpCreationTime)  '将目录的创建时间修改为输入的文件时间
  51. Call LocalFileTimeToFileTime(lptLocalTime, lpLastAccessTime)  '将目录最后访问时间修改为输入的文件时间
  52. Call LocalFileTimeToFileTime(lptLocalTime, lpLastWriteTime)  '将目录最后修改时间修改为输入的文件时间
  53. RetVal = SetFileTime(hDir, lpCreationTime, lpLastAccessTime, lpLastWriteTime)  '设置选定目录的各项时间修改为输入的时间并返回修改成功与否的逻辑值
  54. CloseHandle (hDir)  '关闭目录句柄
  55. SetDirTime = RetVal  '返回修改是否成功
  56. End Function

  57. Sub test()
  58. Dim NewTime As SYSTEMTIME
  59. With NewTime  '设置输入时间(从当前日期时间提取对应内容)
  60.     .wYear = Year(Date)
  61.     .wMonth = Month(Date)
  62.     .wDay = Day(Date)
  63.     .wDayOfWeek = Weekday(Now)
  64.     .wHour = Hour(Now)
  65.     .wMinute = Minute(Now)
  66.     .wSecond = Second(Now)
  67. End With
  68. Call SetDirTime(ThisWorkbook.Path & "\测试", NewTime)  '调用自定义函数修改指定目录各项时间
  69. End Sub
具体详见附件,在当前文件目录下建立"测试"目录供测试.
ModifyDirCreateTime.rar



该帖已经同步到 kevinchengcw的微博
2楼
亡者天下
K哥太厉害了
3楼
迅岐同心
版主一出手,全是精华,顶起!
4楼
JOYARK1958
謝謝提供學習下載中

免责声明

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

评论列表
sitemap