手头有很多照片,想通过拍摄时间来对照片命名,这样方便进行管理。前段时间写了一个类似的程序,但是使用的是照片的创建时间,但有的照片的创建时间和拍摄时间并不一致,因此这次重写了一下这个程序,使用的是照片的拍摄时间来命名。本程序参考了网上的部分代码,如有侵权,请告知。

'*****************************************************************
'Name:filename.frm
'Desc:使用照片的拍摄日期和时间,命名该照片
'Parameter:
'Return:
'Author: yoyoba(stuyou@126.com)
'Date: 2011-8-30
'Modify:2011-8-30
'*****************************************************************
'需要建立一个FileListBox控件和一个CommandButton控件
Private Type GdiplusStartupInput
    GdiplusVersion As Long
    DebugEventCallback As Long
    SuppressBackgroundThread As Long
    SuppressE xternalCodecs As Long
End Type


Private Type PropertyItem
   propId As Long ' ID of this property
   Length As Long ' Length of the property value, in bytes
   Type As Long ' Type of the value, as one of TAG_TYPE_XXX defined above
   Value As Long ' property value
End Type


Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As Long, hImage As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long

Private Declare Function GdipGetPropertyCount Lib "gdiplus" (ByVal Image As Long, numOfProperty As Long) As Long
Private Declare Function GdipGetPropertyIdList Lib "gdiplus" (ByVal Image As Long, ByVal numOfProperty As Long, list As Long) As Long
Private Declare Function GdipGetPropertyItemSize Lib "gdiplus" (ByVal Image As Long, ByVal propId As Long, Size As Long) As Long
Private Declare Function GdipGetPropertyItem Lib "gdiplus" (ByVal Image As Long, ByVal propId As Long, ByVal propSize As Long, Buffer As Long) As Long
Private Declare Function GdipGetPropertySize Lib "gdiplus" (ByVal Image As Long, totalBufferSize As Long, numProperties As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDst As Any, lpSrc As Any, ByVal ByteLength As Long)



Private Function GetPhotoDate(ImagePath As String) As String
    Dim Bitmap As Long
    Dim Token As Long
    Dim Index As Long
    Dim PropertyCount As Long
    Dim ItemSize As Long
    Dim Prop As PropertyItem
    Dim GdipInput As GdiplusStartupInput
    Const PropertyTagExifDTOrig As Long = &H9003& ' Date & time of original

    GdipInput.GdiplusVersion = 1
    GdiplusStartup Token, GdipInput
    GdipLoadImageFromFile StrPtr(ImagePath), Bitmap
    GdipGetPropertyCount Bitmap, PropertyCount
    ReDim PropertyList(PropertyCount - 1) As Long
    GdipGetPropertyIdList Bitmap, PropertyCount, PropertyList(0)
    For Index = 0 To PropertyCount - 1
        GdipGetPropertyItemSize Bitmap, PropertyList(Index), ItemSize
        ReDim Buffer(ItemSize - 1) As Byte
        GdipGetPropertyItem Bitmap, PropertyList(Index), ItemSize, ByVal VarPtr(Buffer(0))
        CopyMemory Prop, ByVal VarPtr(Buffer(0)), Len(Prop)
        ReDim Data(ItemSize - 16) As Byte
        CopyMemory Data(0), ByVal Prop.Value, ItemSize - 16
        Select Case PropertyList(Index)
        Case PropertyTagExifDTOrig
            GetPhotoDate = StrConv(Data, vbUnicode)
        End Select
    Next
    GdipDisposeImage Bitmap
    GdiplusShutdown Token
End Function


Private Sub Command1_Click()
    Dim filename, newfilename As String, filedt As String, exname As String
    Dim i%
    For i = 0 To File1.ListCount - 1
        File1.ListIndex = i
        filename = CStr(File1.filename)
        exname = Right(filename, 4)
        
        filedt = GetPhotoDate("E:\Photo\" & filename)
        filedt = Left(filedt, 19)
        'MsgBox filedt
        newfilename = getdtname(filedt)
        newfilename = newfilename & exname
        Name "E:\Photo\" & filename As "E:\Photo\" & newfilename
    Next i
End Sub


Private Sub Form_Load()
    File1.Path = "E:\Photo\"
End Sub
Private Function getdtname(dt As String) As String
    '文件的创建时间格式为2010:05:13 19:30:23,即为形参dt。
    '通过getdtname函数得到以文件创建时间为名字的文件名,格式为20100513193023
    Dim dtarray() As String, darray() As String, tarray() As String
    If Len(dt) > 0 Then
        dtarray() = Split(dt)
        'MsgBox dtarray(0)
        'MsgBox dtarray(1)
        darray() = Split(dtarray(0), ":")
        'MsgBox darray(0)
        'MsgBox darray(1)
        'MsgBox darray(2)
        tarray() = Split(dtarray(1), ":")
        'MsgBox tarray(0)
        'MsgBox tarray(1)
        'MsgBox tarray(2)
        If Len(darray(1)) = 1 Then darray(1) = 0 & darray(1)
        If Len(darray(2)) = 1 Then darray(2) = 0 & darray(2)
        If Len(tarray(0)) = 1 Then tarray(0) = 0 & tarray(0)
        If Len(tarray(1)) = 1 Then tarray(1) = 0 & tarray(1)
        If Len(tarray(2)) = 1 Then tarray(2) = 0 & tarray(2)
        getdtname = darray(0) & darray(1) & darray(2) & tarray(0) & tarray(1) & tarray(2)
    End If
End Function