第2个回答 2009-04-04
'tonkeys qq:58507961
'功能:将文件名更改为文件的修改日期
'2009.4.4
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private 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
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Function GetFileChangeDate(ByVal OldFileName As String) As Date
'函数,取得文件的修改日期
Dim fTime(0 To 2) As FILETIME
Dim sTime As SYSTEMTIME
Dim RET As OFSTRUCT
A = OpenFile(OldFileName, RET, 1) '打开文件
b = GetFileTime(A, fTime(0), fTime(1), fTime(2)) '取时间
FileTimeToSystemTime fTime(1), sTime '转换为系统时间
GetFileChangeDate = CDate(sTime.wYear & "-" & sTime.wMonth & "-" & sTime.wDay & " " & sTime.wHour & ":" & sTime.wMinute & ":" & sTime.wSecond)
CloseHandle A '关闭文件
End Function
Private Sub Command1_Click()
File1.Pattern = "*.amr"
File1.Path = "x:\Test" '文件所在目录
filepath = IIf(Right(File1.Path, 1) = "\", File1.Path, File1.Path & "\") '文件所在路径
For i = 0 To File1.ListCount - 1 '遍历文件
oldfile = filepath & File1.List(i) '带路径的旧文件名
newfile = filepath & Format(GetFileChangeDate(oldfile), "yyyy年m月d日_hh时nn分ss秒_") & ".AMR '带路径的新文件名"
FileCopy oldfile, newfile '更名
Kill oldfile '删除原文件 ,理论上可以用Name oldfile as newfile 代替这两行
Next i
End Sub