|
VBA 更改照片文件日期_百度搜索 http://www.baidu.com/s?ie=utf-8& ... 6%E6%97%A5%E6%9C%9F
不能运行
'Private Declare PtrSafe Function SetFileTime Lib "kernel32" (ByVal hFile As LongPtr, lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As LongPtr
'Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hFile As LongPtr) As LongPtr
'Private Declare PtrSafe Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As LongPtr) As LongPtr
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Public Function SetFileDate(filePath As String, ByVal dtModified As Date) As Boolean
Dim hFile As LongPtr
Dim ftModified As FILETIME
' 将日期转换为FILETIME结构
DateTimeToFileTime dtModified, ftModified
' 打开文件以便修改日期
hFile = CreateFile(filePath, GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If hFile <> 0 Then
' 设置文件修改日期
If SetFileTime(hFile, ByVal 0, ftModified, ftModified) Then
SetFileDate = True
End If
' 关闭文件
CloseHandle hFile
End If
End Function
' 辅助结构
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
' 将Date转换为FILETIME
'Private Declare PtrSafe Sub DateTimeToFileTime Lib "kernel32" (ByVal lpDateTime As Date, lpFileTime As FILETIME)'
'使用这个函数,你可以将图片文件的修改日期更改为任何指定的日期:
Sub ChangePhotoDate()
Dim photoPath As String
Dim newDate As Date
' 图片文件路径
photoPath = "C:\1.jpg"
' 新的日期
newDate = DateSerial(2022, 1, 1) ' 示例日期:2022年1月1日
' 更改文件日期
If SetFileDate(photoPath, newDate) Then
MsgBox "文件日期已更改。"
Else
MsgBox "无法更改文件日期。"
End If
End Sub
|
|