|
- Option Explicit
- Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
- Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
- Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FileTime, lpLastAccessTime As FileTime, lpLastWriteTime As FileTime) As Long
- Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FileTime) As Long
- 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
- Type FileTime
- dwLowDateTime As Long
- dwHighDateTime As Long
- End Type
- Type OFSTRUCT
- cBytes As Byte
- fFixedDisk As Byte
- nErrCode As Integer
- Reserved1 As Integer
- Reserved2 As Integer
- szPathName(128) As Byte
- End Type
- '***************************************************************************************
- '功能:修改文件时间
- '参数:fnm 文本型 文件的完整路径及名称
- ' crtt/mdft/acct 分别为创建时间/修改时间/访问时间 日期型 可选(不选默认为不修改)
- '说明:修改成功返回0,失败返回非0。
- ' 注意,本函数不判断文件的只读特性,如文件是只读的,不修改,返回0。
- '***************************************************************************************
- Function ModifyFileTime(ByVal fnm As String, Optional ByVal crtt As Date, Optional ByVal mdft As Date, Optional ByVal acct As Date) As Boolean
- On Error Resume Next
- Dim fhd&
- Dim ofs As OFSTRUCT
- Dim st1 As SYSTEMTIME, st2 As SYSTEMTIME, st3 As SYSTEMTIME
- Dim ft1 As FileTime, ft2 As FileTime, ft3 As FileTime
- If Dir(fnm) = "" Then
- ModifyFileTime = 1
- Exit Function
- End If
- fhd = OpenFile(fnm, ofs, 2)
- If crtt <> TimeValue("0:00:00") Then
- With st1
- crtt = crtt - TimeSerial(8, 0, 0)
- .wYear = Year(crtt)
- .wMonth = Month(crtt)
- .wDay = Day(crtt)
- .wHour = Hour(crtt)
- .wMinute = Minute(crtt)
- .wSecond = Second(crtt)
- End With
- SystemTimeToFileTime st1, ft1
- End If
- If mdft <> TimeValue("0:00:00") Then
- With st2
- mdft = mdft - TimeSerial(8, 0, 0)
- .wYear = Year(mdft)
- .wMonth = Month(mdft)
- .wDay = Day(mdft)
- .wHour = Hour(mdft)
- .wMinute = Minute(mdft)
- .wSecond = Second(mdft)
- End With
- SystemTimeToFileTime st2, ft2
- End If
- If acct <> TimeValue("0:00:00") Then
- With st3
- acct = acct - TimeSerial(8, 0, 0)
- .wYear = Year(acct)
- .wMonth = Month(acct)
- .wDay = Day(acct)
- .wHour = Hour(acct)
- .wMinute = Minute(acct)
- .wSecond = Second(acct)
- End With
- SystemTimeToFileTime st3, ft3
- End If
- SetFileTime fhd, ft1, ft2, ft3
- CloseHandle (fhd)
- ext:
- ModifyFileTime = Err.Number > 0
- Err.Clear
- End Function
- Sub aTest()
- ModifyFileTime "d:\ls.txt", Now
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|