ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

如何修改相片拍摄时间

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-2-10 16:43 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
我水平实在有限,没有办法,恳求大神出现。帮忙

TA的精华主题

TA的得分主题

发表于 2015-12-3 12:37 | 显示全部楼层
wcymiss 发表于 2015-2-9 13:18
楼主试试,下面的代码在我的电脑上修改成功:

自定义函数:调用方法:

老师,arrOldDate = StrConv(Format(strOldDate, "yyyy:mm:dd hh:nn"), vbFromUnicode) 获取的旧日期是空的,就是普通jpg文件

TA的精华主题

TA的得分主题

发表于 2015-12-4 07:58 | 显示全部楼层
renahu 发表于 2015-12-3 12:37
老师,arrOldDate = StrConv(Format(strOldDate, "yyyy:mm:dd hh:nn"), vbFromUnicode) 获取的旧日期是空 ...

普通jpg文件哪有拍摄日期呢?

TA的精华主题

TA的得分主题

发表于 2015-12-4 09:04 | 显示全部楼层
wxhnr 发表于 2015-12-4 07:58
普通jpg文件哪有拍摄日期呢?

谢谢老师提醒,成功了,不过我的拍摄日期的序号是3,不是25,可能不同相机拍的都不一样吧

TA的精华主题

TA的得分主题

发表于 2016-8-23 20:19 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
wcymiss 发表于 2015-2-9 13:18
楼主试试,下面的代码在我的电脑上修改成功:

自定义函数:调用方法:

修改一个是实现了,修改多个怎么调用?

TA的精华主题

TA的得分主题

发表于 2024-4-10 09:59 | 显示全部楼层
image.png


'第25项是照片的拍摄日期,不知道不同的配置是否相同
strOldDate = objFolder.GetDetailsOf(objFolderItem, 25)






  1. Function ModiPhotoDate(strPath As Variant, strFile As String, strNewDate As String)
  2.     'strPath:文件所在文件夹名,必须是Variant类型
  3.     'strFile:文件名(不包括路径)
  4.     'strNewDate:要显示的日期时间,yyyy-mm-dd hh:nn格式,时间不写的话用原照片时间
  5.    
  6.     Dim objShell As Object
  7.     Dim objFolder As Object
  8.     Dim objFolderItem As Object
  9.     Dim strOldDate As String
  10.     Dim arrOldDate() As Byte
  11.     Dim arrNewDate() As Byte
  12.     Dim arrByte() As Byte
  13.     Dim strNewFile As String
  14.     Dim i As Long
  15.     Dim j As Long
  16.    
  17.     '照片复件文件名
  18.     strNewFile = Left(strFile, InStrRev(strFile, ".") - 1) & "复件." & Mid(strFile, InStrRev(strFile, ".") + 1)
  19.    
  20.     '获取照片的拍摄日期时间
  21.     Set objShell = CreateObject("Shell.application")
  22.     Set objFolder = objShell.Namespace(strPath)
  23.     Set objFolderItem = objFolder.ParseName(strFile)
  24.     '第25项是照片的拍摄日期,不知道不同的配置是否相同
  25.     strOldDate = objFolder.GetDetailsOf(objFolderItem, 25)
  26.     Set objFolderItem = Nothing
  27.     Set objFolder = Nothing
  28.     Set objShell = Nothing
  29.    
  30.     If Len(strNewDate) <= 11 Then
  31.         strNewDate = Trim(strNewDate) & Format(strOldDate, " hh:nn")
  32.     End If
  33.    
  34.     '日期在照片二进制中是"yyyy:mm:dd hh:nn:ss"格式存在,不知道不同格式的照片是否相同
  35.     arrOldDate = StrConv(Format(strOldDate, "yyyy:mm:dd hh:nn"), vbFromUnicode)
  36.     arrNewDate = StrConv(Format(strNewDate, "yyyy:mm:dd hh:nn"), vbFromUnicode)
  37.    
  38.     '读入照片的二进制数据
  39.     With CreateObject("Adodb.Stream")
  40.         .Open
  41.         .Type = 1 'adTypeBinary
  42.         .LoadFromFile strPath & "" & strFile
  43.         arrByte = .read
  44.         .Close
  45.     End With
  46.    
  47.     '修改日期时间
  48.     For i = 0 To UBound(arrByte)
  49.         If arrByte(i) = arrOldDate(0) Then
  50.             For j = 1 To UBound(arrOldDate)
  51.                 If arrByte(i + j) <> arrOldDate(j) Then Exit For
  52.             Next
  53.             If j > UBound(arrOldDate) Then
  54.                 For j = 0 To UBound(arrOldDate)
  55.                     arrByte(i + j) = arrNewDate(j)
  56.                 Next
  57.             End If
  58.         End If
  59.     Next
  60.    
  61.     '二进制数据写入文件,创建日期和修改日期等是当前日期
  62.     With CreateObject("Adodb.Stream")
  63.         .Type = 1 'adTypeBinary
  64.         .Open
  65.         .Write arrByte
  66.         .SaveToFile strPath & "" & strNewFile, 2 'adSaveCreateOverWrite
  67.         .Close
  68.     End With
  69.    
  70. End Function

  71. Sub Main()
  72.     Call ModiPhotoDate("C:", "1.JPG", "2014-12-01")
  73. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2024-4-10 10:16 | 显示全部楼层

VBA 更改照片文件日期_百度搜索  http://www.baidu.com/s?ie=utf-8& ... 6%E6%97%A5%E6%9C%9F


不能运行

image.png







'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


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-9-28 03:27 , Processed in 0.038912 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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