|
楼主 |
发表于 2024-4-14 14:36
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
学习学习再学习,一个一个帖子试。
VB如何修改文件的创建时间?-CSDN社区 https://bbs.csdn.net/topics/70403822
- Public Type FILETIME
- dwLowDateTime As Long
- dwHighDateTime As Long
- End Type
- Public Const OFS_MAXPATHNAME = 128
- Public Type OFSTRUCT
- cBytes As Byte
- fFixedDisk As Byte
- nErrCode As Integer
- Reserved1 As Integer
- Reserved2 As Integer
- szPathName(OFS_MAXPATHNAME) As Byte
- End Type
- Public Const OF_READ = &H0
- Public Const OF_READWRITE = &H2
- 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
- Public Type TIME_ZONE_INFORMATION
- bias As Long
- StandardName(32) As Integer
- StandardDate As SYSTEMTIME
- StandardBias As Long
- DaylightName(32) As Integer
- DaylightDate As SYSTEMTIME
- DaylightBias As Long
- End Type
- Public Type BY_HANDLE_FILE_INFORMATION
- dwFileAttributes As Long
- ftCreationTime As FILETIME
- ftLastAccessTime As FILETIME
- ftLastWriteTime As FILETIME
- dwVolumeSerialNumber As Long
- nFileSizeHigh As Long
- nFileSizeLow As Long
- nNumberOfLinks As Long
- nFileIndexHigh As Long
- nFileIndexLow As Long
- End Type
- Public Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
- Public Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
- Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
- Public Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
- Public Declare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
- Public Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
- Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
- Dim FileHandle As Long
- Dim OpenBuff As OFSTRUCT
- Dim tZone As TIME_ZONE_INFORMATION
- Dim sCreate As SYSTEMTIME
- Dim sAccess As SYSTEMTIME
- Dim sWrite As SYSTEMTIME
- Dim fCreate As FILETIME
- Dim fAccess As FILETIME
- Dim fWrite As FILETIME
- Dim bias As Long
- Dim theTime As Date
- Dim FileInfo As BY_HANDLE_FILE_INFORMATION
- Public Enum FileTimeOptions
- theCreateTime = 1
- theLastAccessTime = 2
- theLastWriteTime = 3
- End Enum
- Public Sub SetTime(ByVal FileName As String, ByVal CreateTime As Date, ByVal LastAccessTime As Date, ByVal LastWriteTime As Date)
- '1,处理文件属性,去掉只读等属性,
- If Dir(FileName, 63) = "" Then Exit Sub
- SetAttr FileName, vbNormal
- '2,处理时差
- GetTimeZoneInformation tZone
- bias = tZone.bias '时差,以分钟为单位,中国地区为早8小时,值-480。
- theTime = TimeSerial(0, bias, 0)
- CreateTime = CreateTime + theTime
- LastAccessTime = LastAccessTime + theTime
- LastWriteTime = LastWriteTime + theTime
- '3,将SYSTEMTIME格式时间转换为FILETIME格式时间
- sCreate.wDay = Day(CreateTime): sCreate.wHour = Hour(CreateTime): sCreate.wMinute = Minute(CreateTime): sCreate.wMonth = Month(CreateTime): sCreate.wSecond = Second(CreateTime): sCreate.wYear = Year(CreateTime)
- sAccess.wDay = Day(LastAccessTime): sAccess.wHour = Hour(LastAccessTime): sAccess.wMinute = Minute(LastAccessTime): sAccess.wMonth = Month(LastAccessTime): sAccess.wSecond = Second(LastAccessTime): sAccess.wYear = Year(LastAccessTime)
- sWrite.wDay = Day(LastWriteTime): sWrite.wHour = Hour(LastWriteTime): sWrite.wMinute = Minute(LastWriteTime): sWrite.wMonth = Month(LastWriteTime): sWrite.wSecond = Second(LastWriteTime): sWrite.wYear = Year(LastWriteTime)
- SystemTimeToFileTime sCreate, fCreate
- SystemTimeToFileTime sAccess, fAccess
- SystemTimeToFileTime sWrite, fWrite
- '4,修改文件时间
- FileHandle = OpenFile(FileName, OpenBuff, OF_READWRITE)
- SetFileTime FileHandle, fCreate, fAccess, fWrite '注:这里的三个时间采用的是格林尼治标准时间!
- CloseHandle FileHandle
- End Sub
- Public Function GetTime(ByVal FileName As String, Optional ByVal TimeOfFile As FileTimeOptions = theCreateTime) As Date
- If Dir(FileName, 63) = "" Then Exit Function
- FileHandle = OpenFile(FileName, OpenBuff, OF_READ)
- GetFileInformationByHandle FileHandle, FileInfo
- CloseHandle FileHandle
- GetTimeZoneInformation tZone
- bias = tZone.bias
- FileTimeToSystemTime FileInfo.ftCreationTime, sCreate
- FileTimeToSystemTime FileInfo.ftLastAccessTime, sAccess
- FileTimeToSystemTime FileInfo.ftLastWriteTime, sWrite
- Dim CurTime As SYSTEMTIME
- Select Case TimeOfFile
- Case 1
- CurTime = sCreate
- Case 2
- CurTime = sAccess
- Case 3
- CurTime = sWrite
- End Select
- GetTime = DateSerial(CurTime.wYear, CurTime.wMonth, CurTime.wDay) + TimeSerial(CurTime.wHour, CurTime.wMinute - bias, CurTime.wSecond)
- End Function
- Sub ll()
-
- Dim oDate1 As Date, oDate2 As Date, oDate3 As Date
- oDate1 = "1939/1/1"
- oDate2 = "1949/1/1"
- oDate3 = "1959/1/1"
- SetTime "c:\1.jpg", oDate1, oDate2, oDate3
-
-
- End Sub
复制代码
|
|