ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 14073|回复: 17

[其他资源] 【API 例子】更改檔案時間

[复制链接]

TA的精华主题

TA的得分主题

发表于 2003-1-19 11:36 | 显示全部楼层 |阅读模式
其實 Windows 檔案總管更改時問是很簡單,這是API例子 Option Explicit 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 Const GENERIC_WRITE = &H40000000 Private Const OPEN_EXISTING = 3 Private Const FILE_SHARE_READ = &H1 Private Const FILE_SHARE_WRITE = &H2 Private Const sPromptDate As String = "Enter date (DD-MM-YYYY format)" Private Const sTitleDate As String = "Date Entry" Private Const sPromptTime As String = "Enter time [Optional](HH:MM:SS format)" Private Const sTitleTime As String = "Time Entry" 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 SetFileTime Lib "kernel32" _ (ByVal hFile As Long, _ lpCreationTime As FILETIME, _ lpLastAccessTime As FILETIME, _ 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 Private Declare Function LocalFileTimeToFileTime Lib "kernel32" ( _ lpLocalFileTime As FILETIME, _ lpFileTime As FILETIME) As Long '*----------------------------------------------------------------------- '* This routine sets the date and option ally the time of the '* specified file. '* '* Inputs: '* sFileName; String containing the full path to the file '* sDate; String containing the date (DD-MM-YYYY format) '* sTime; (Optional) string containing the time in HH:MM:SS format '* Thanks to the KPDTeam '* Mods by Ivan F Moala 12/11/2002 '*----------------------------------------------------------------------- Private Function SetFileDate(ByVal sFileName As String, ByVal sDate As String, _ Optional sTime As String = "00:00:00") As Boolean Dim dDate As Date Dim udtFileTime As FILETIME Dim udtLocalTime As FILETIME Dim udtSystemTime As SYSTEMTIME Dim lFileHandle As Long Dim RetVal As Long '// Make sure we format it properly before converting dDate = format(sDate & " " & sTime, "DD-MM-YYYY HH:MM:SS") With udtSystemTime .wYear = Year(dDate) .wMonth = Month(dDate) .wDay = Day(dDate) .wDayOfWeek = Weekday(dDate) - 1 .wHour = Hour(dDate) .wMinute = Minute(dDate) .wSecond = Second(dDate) .wMilliseconds = 0 End With '// Convert system time to local time SystemTimeToFileTime udtSystemTime, udtLocalTime '// Convert local time to GMT (UTC Universal Time Coordinates) '// I prefer to call it GMT Greenwich Mean Time LocalFileTimeToFileTime udtLocalTime, udtFileTime '// Open the file for Read/Write to get the filehandle lFileHandle = CreateFile( _ sFileName, _ GENERIC_WRITE, _ FILE_SHARE_READ Or FILE_SHARE_WRITE, _ ByVal 0&, _ OPEN_EXISTING, _ 0, _ 0) '// Do we have a handle? If lFileHandle = 0 Then SetFileDate = False: GoTo ExitProperly '// Change date/time property of the file RetVal = SetFileTime(lFileHandle, udtFileTime, udtFileTime, udtFileTime) '// Was it changed? If RetVal = 0 Then SetFileDate = False: GoTo ExitProperly SetFileDate = True ExitProperly: '// Close the file handle CloseHandle lFileHandle End Function Sub Tester() Dim OK As Boolean Dim strMyFileName As String Dim strMyDate As String Dim strMyTime As String Dim objCal As Object strMyFileName = Application.GetOpenFilename("Any File (*.*), *.*") If strMyFileName = "Fasle" Then Exit Sub 'format = DD-MM-YYYY DateAgain: strMyDate = Application.InputBox(sPromptDate, sTitleDate, Type:=2) If strMyDate = "False" Then Exit Sub If Not VerifydateTime(strMyDate, True) Then GoTo DateAgain 'format = HH:MM:SS TimeAgain: strMyTime = Application.InputBox(sPromptTime, sTitleTime, Type:=2) If strMyTime = "False" Then Exit Sub If Len(strMyTime) = 0 Then GoTo Process If Not VerifydateTime(strMyTime, False) Then GoTo TimeAgain Process: OK = SetFileDate(strMyFileName, strMyDate, strMyTime) MsgBox strMyFileName & " Date changed:=" & OK End Sub Function VerifydateTime(sDate As String, DateOrTime As Boolean) As Boolean Select Case DateOrTime Case True If Len(sDate) <> 10 Then VerifydateTime = False: Exit Function On Error Resume Next If Not IsDate(CVDate(sDate)) Then '// VerifydateTime = False Exit Function End If If MsgBox(CVDate(sDate) & " OK", vbYesNo) = vbNo Then VerifydateTime = False Exit Function Else VerifydateTime = True End If Case Else If Len(sDate) <> 8 Then VerifydateTime = False: Exit Function On Error Resume Next If MsgBox(Timevalue(sDate) & " OK", vbYesNo) = vbNo Then VerifydateTime = False Exit Function Else VerifydateTime = True End If End Select End Function

TA的精华主题

TA的得分主题

发表于 2006-5-16 14:42 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-5-18 20:32 | 显示全部楼层

一年之后再来看 !

再不行就 ~~~~~

头好晕 [em06][em06][em06]

TA的精华主题

TA的得分主题

发表于 2006-5-20 21:01 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

不错!

TA的精华主题

TA的得分主题

发表于 2006-5-22 08:40 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

正在學API,研究中

TA的精华主题

TA的得分主题

发表于 2006-5-28 17:01 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-6-3 00:22 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

高手,给写点注释啊

[em05][em06][em06]

TA的精华主题

TA的得分主题

发表于 2006-6-9 13:55 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2007-4-24 13:07 | 显示全部楼层
外语盲一个,看的我头都晕,要等我学好英语以后再来顶贴.

TA的精华主题

TA的得分主题

发表于 2007-5-5 13:12 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-24 09:27 , Processed in 0.037862 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表