问题的关键是获取文件的“时间”(创建时间、最后修改时间还是最后访问时间?)。给你个例子:'引用 Microsoft Scripting RuntimeDim objFSO As New FileSystemObject Dim objFileDetails As File ' Identify the file for which you want ' to display properties Set objFileDetails = _ objFSO.GetFile("C:\config.sys")Debug.Print "File Type:" & objFileDetails.Type Debug.Print "Date Created:" & objFileDetails.DateCreated Debug.Print "Date Modified:" & objFileDetails.DateLastModified Debug.Print "Date Accessed:" & objFileDetails.DateLastAccessed你需要添加的是,用 Dir 函数循环获取文件夹下的文件,以及将找到符合条件的文件复制。
支持! 此外还可以使用API函数获取文件的时间,同样实现这个功能。
'Example Name: File's Created, Accessed and Modified Dates'------------------------------------------------------------------------------ ' ' BAS Moudel Code ' '------------------------------------------------------------------------------ Option ExplicitPublic Const OFS_MAXPATHNAME = 260 Public Const OF_READWRITE = &H2Public Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End TypePublic Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End TypePublic 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 TypePublic 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 TypePublic Declare Sub GetLocalTime Lib "kernel32" _ (lpSystemTime As SYSTEMTIME)Public Declare Function GetFileTime Lib "kernel32" _ (ByVal hFile As Long, lpCreationTime As FILETIME, _ lpLastAccessTime As FILETIME, _ lpLastWriteTime As FILETIME) As Long
Public Declare Function SetFileTime Lib "kernel32" _ (ByVal hFile As Long, _ lpCreationTime As FILETIME, _ lpLastAccessTime As FILETIME, _ lpLastWriteTime As FILETIME) As LongPublic Declare Function FileTimeToSystemTime Lib "kernel32" _ (lpFileTime As FILETIME, _ lpSystemTime As SYSTEMTIME) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" _ (lpSystemTime As SYSTEMTIME, _ lpFileTime As FILETIME) As LongPublic Declare Function OpenFile Lib "kernel32" _ (ByVal lpFileName As String, _ lpReOpenBuff As OFSTRUCT, _ ByVal wStyle As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" _ (ByVal hFile As Long) As LongPublic Type SHELLEXECUTEINFO cbSize As Long fMask As Long hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End TypePublic Const SEE_MASK_INVOKEIDLIST = &HC Public Const SEE_MASK_NOCLOSEPROCESS = &H40 Public Const SEE_MASK_FLAG_NO_UI = &H400Public Declare Function ShellExecuteEx Lib "shell32" _ Alias "ShellExecuteEx" _ (SEI As SHELLEXECUTEINFO) As Long Public Function GetFileDateString(CT As FILETIME) As String Dim ST As SYSTEMTIME Dim ds As Single
'convert the passed FILETIME to a 'valid SYSTEMTIME format for display If FileTimeToSystemTime(CT, ST) Then ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay) GetFileDateString = Format$(ds, "DDDD MMMM D, YYYY") Else: GetFileDateString = "" End IfEnd Function Public Function GetSystemDateString(ST As SYSTEMTIME) As String Dim ds As Single
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
If ds Then GetSystemDateString = Format$(ds, "DDDD MMMM D, YYYY") Else: GetSystemDateString = "error!" End IfEnd Function '--end block--''------------------------------------------------------------------------------ ' ' Form Code ' '------------------------------------------------------------------------------ Option ExplicitPrivate Sub Command2_Click() 'variables required Dim hFile As Long Dim fName As String Dim tmp As String
'structures required Dim OFS As OFSTRUCT Dim SYS_TIME As SYSTEMTIME Dim FT_CREATE As FILETIME Dim FT_ACCESS As FILETIME Dim FT_WRITE As FILETIME Dim NEW_TIME As FILETIME
'assign the textbox entry to the filename fName = (Text1)
'open the file hFile = OpenFile(fName, OFS, OF_READWRITE)
'get the FILETIME info for the created, 'accessed and last write info Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only --------------------------- 'show the system time info tmp = "Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf tmp = tmp & "Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf tmp = tmp & "Last Modified:" & vbTab & GetFileDateString(FT_WRITE) Text2.Text = tmp '--------------------------------------------
'obtain the local system time '(adjusts for the GMT deviation 'of the local time zone) GetLocalTime SYS_TIME
'convert the system time to a valid file time Call SystemTimeToFileTime(SYS_TIME, NEW_TIME)
'set the created, accessed and modified dates all 'to the new dates. A null (0&) could be passed as 'any of the NEW_TIME parameters to leave that date unchanged. Call SetFileTime(hFile, NEW_TIME, NEW_TIME, NEW_TIME)
're-read the updated FILETIME info for the created, 'accessed and last write info Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only --------------------------- 'show the system time info tmp = "New Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf tmp = tmp & "New Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf tmp = tmp & "New Last Modified:" & vbTab & GetFileDateString(FT_WRITE) Text4.Text = tmp '--------------------------------------------
'clean up by closing the file Call CloseHandle(hFile)End SubPrivate Sub Command1_Click() Unload Me
End Sub Private Sub Command3_Click() Dim SEI As SHELLEXECUTEINFO
'Fill in the SHELLEXECUTEINFO structure 'and call the ShellExecuteEx API With SEI .cbSize = Len(SEI) .fMask = SEE_MASK_NOCLOSEPROCESS Or _ SEE_MASK_INVOKEIDLIST Or _ SEE_MASK_FLAG_NO_UI .hwnd = Me.hwnd .lpVerb = "properties" .lpFile = (Text1) .lpParameters = vbNullChar .lpDirectory = vbNullChar .nShow = 0 .hInstApp = 0 .lpIDList = 0 End With
用这个方式就好了Private Sub Command1_Click() Dim FindFileName As String, FindFileTime As Date FindFileByPathBeTweenTime "c:\", "2011年9月23日 16:10:22", "2011年9月23日 16:13:00", FindFileName, FindFileTime MsgBox "找到文件:" & FindFileName & vbCrLf & "文件日期:" & FindFileTime End Sub Function FindFileByPathBeTweenTime(Path1 As String, Time1 As Date, Time2 As Date, Optional FindFileName As String, Optional FindFileTime As Date) As String 'Path1要求\字符结尾 Dim FileName As String FileName = Dir(Path1) While FileName <> "" If FileDateTime(Path1 & FileName) >= Time1 And FileDateTime(Path1 & FileName) <= Time2 Then FindFileTime = FileDateTime(Path1 & FileName) FindFileName = FileName FindFileByPathBeTweenTime = FileName Exit Function End If FileName = Dir Wend End Function
Time1 As Date, Time2 As Date,这2个是输入几号到几号的日期,
然后可以用filecopy进行复制了'┏〓〓〓〓〓〓〓〓〓 FindFileByPathBeTweenTime,start 〓〓〓〓〓〓〓〓〓┓ '[简介]: '查找目录下时间介于2个时间之间的一个文件 Function FindFileByPathBeTweenTime(Path1 As String, Time1 As Date, Time2 As Date, Optional FindFileName As String, Optional FindFileTime As Date) As String '[mycode_id:2067],edittime:2011-9-30 下午 08:26:44 'Path1要求\字符结尾 Dim FileName As String FileName = Dir(Path1) While FileName <> "" If FileDateTime(Path1 & FileName) >= Time1 And FileDateTime(Path1 & FileName) <= Time2 Then FindFileTime = FileDateTime(Path1 & FileName) FindFileName = FileName FindFileByPathBeTweenTime = FileName Exit Function End If FileName = Dir Wend End Function '┗〓〓〓〓〓〓〓〓〓 FindFileByPathBeTweenTime,end 〓〓〓〓〓〓〓〓〓┛
Dim objFileDetails As File
' Identify the file for which you want
' to display properties
Set objFileDetails = _
objFSO.GetFile("C:\config.sys")Debug.Print "File Type:" & objFileDetails.Type
Debug.Print "Date Created:" & objFileDetails.DateCreated
Debug.Print "Date Modified:" & objFileDetails.DateLastModified
Debug.Print "Date Accessed:" & objFileDetails.DateLastAccessed你需要添加的是,用 Dir 函数循环获取文件夹下的文件,以及将找到符合条件的文件复制。
此外还可以使用API函数获取文件的时间,同样实现这个功能。
'Example Name: File's Created, Accessed and Modified Dates'------------------------------------------------------------------------------
'
' BAS Moudel Code
'
'------------------------------------------------------------------------------
Option ExplicitPublic Const OFS_MAXPATHNAME = 260
Public Const OF_READWRITE = &H2Public Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End TypePublic Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End TypePublic 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 TypePublic 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 TypePublic Declare Sub GetLocalTime Lib "kernel32" _
(lpSystemTime As SYSTEMTIME)Public Declare Function GetFileTime Lib "kernel32" _
(ByVal hFile As Long, lpCreationTime As FILETIME, _
lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As Long
Public Declare Function SetFileTime Lib "kernel32" _
(ByVal hFile As Long, _
lpCreationTime As FILETIME, _
lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As LongPublic Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FILETIME, _
lpSystemTime As SYSTEMTIME) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" _
(lpSystemTime As SYSTEMTIME, _
lpFileTime As FILETIME) As LongPublic Declare Function OpenFile Lib "kernel32" _
(ByVal lpFileName As String, _
lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hFile As Long) As LongPublic Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End TypePublic Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400Public Declare Function ShellExecuteEx Lib "shell32" _
Alias "ShellExecuteEx" _
(SEI As SHELLEXECUTEINFO) As Long
Public Function GetFileDateString(CT As FILETIME) As String Dim ST As SYSTEMTIME
Dim ds As Single
'convert the passed FILETIME to a
'valid SYSTEMTIME format for display
If FileTimeToSystemTime(CT, ST) Then
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
GetFileDateString = Format$(ds, "DDDD MMMM D, YYYY")
Else: GetFileDateString = ""
End IfEnd Function
Public Function GetSystemDateString(ST As SYSTEMTIME) As String Dim ds As Single
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay)
If ds Then
GetSystemDateString = Format$(ds, "DDDD MMMM D, YYYY")
Else: GetSystemDateString = "error!"
End IfEnd Function
'--end block--''------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option ExplicitPrivate Sub Command2_Click() 'variables required
Dim hFile As Long
Dim fName As String
Dim tmp As String
'structures required
Dim OFS As OFSTRUCT
Dim SYS_TIME As SYSTEMTIME
Dim FT_CREATE As FILETIME
Dim FT_ACCESS As FILETIME
Dim FT_WRITE As FILETIME
Dim NEW_TIME As FILETIME
'assign the textbox entry to the filename
fName = (Text1)
'open the file
hFile = OpenFile(fName, OFS, OF_READWRITE)
'get the FILETIME info for the created,
'accessed and last write info
Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only ---------------------------
'show the system time info
tmp = "Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf
tmp = tmp & "Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf
tmp = tmp & "Last Modified:" & vbTab & GetFileDateString(FT_WRITE)
Text2.Text = tmp
'--------------------------------------------
'obtain the local system time
'(adjusts for the GMT deviation
'of the local time zone)
GetLocalTime SYS_TIME
'----- debug only ---------------------------
'show the system time info
tmp = ""
tmp = "Day:" & vbTab & SYS_TIME.wDay & vbCrLf
tmp = tmp & "Month:" & vbTab & SYS_TIME.wMonth & vbCrLf
tmp = tmp & "Year:" & vbTab & SYS_TIME.wYear & vbCrLf
tmp = tmp & "String:" & vbTab & GetSystemDateString(SYS_TIME)
Text3.Text = tmp
'--------------------------------------------
'convert the system time to a valid file time
Call SystemTimeToFileTime(SYS_TIME, NEW_TIME)
'set the created, accessed and modified dates all
'to the new dates. A null (0&) could be passed as
'any of the NEW_TIME parameters to leave that date unchanged.
Call SetFileTime(hFile, NEW_TIME, NEW_TIME, NEW_TIME)
're-read the updated FILETIME info for the created,
'accessed and last write info
Call GetFileTime(hFile, FT_CREATE, FT_ACCESS, FT_WRITE)
'----- debug only ---------------------------
'show the system time info
tmp = "New Date Created:" & vbTab & GetFileDateString(FT_CREATE) & vbCrLf
tmp = tmp & "New Last Access:" & vbTab & GetFileDateString(FT_ACCESS) & vbCrLf
tmp = tmp & "New Last Modified:" & vbTab & GetFileDateString(FT_WRITE)
Text4.Text = tmp
'--------------------------------------------
'clean up by closing the file
Call CloseHandle(hFile)End SubPrivate Sub Command1_Click() Unload Me
End Sub
Private Sub Command3_Click() Dim SEI As SHELLEXECUTEINFO
'Fill in the SHELLEXECUTEINFO structure
'and call the ShellExecuteEx API
With SEI
.cbSize = Len(SEI)
.fMask = SEE_MASK_NOCLOSEPROCESS Or _
SEE_MASK_INVOKEIDLIST Or _
SEE_MASK_FLAG_NO_UI
.hwnd = Me.hwnd
.lpVerb = "properties"
.lpFile = (Text1)
.lpParameters = vbNullChar
.lpDirectory = vbNullChar
.nShow = 0
.hInstApp = 0
.lpIDList = 0
End With
'call the API
Call ShellExecuteEx(SEI)End Sub
'读d:\allfbyt.txt文件中按日期倒排序的d:\mydir\*.*文件日期和文件名
Dim FindFileName As String, FindFileTime As Date
FindFileByPathBeTweenTime "c:\", "2011年9月23日 16:10:22", "2011年9月23日 16:13:00", FindFileName, FindFileTime
MsgBox "找到文件:" & FindFileName & vbCrLf & "文件日期:" & FindFileTime
End Sub
Function FindFileByPathBeTweenTime(Path1 As String, Time1 As Date, Time2 As Date, Optional FindFileName As String, Optional FindFileTime As Date) As String
'Path1要求\字符结尾
Dim FileName As String
FileName = Dir(Path1)
While FileName <> ""
If FileDateTime(Path1 & FileName) >= Time1 And FileDateTime(Path1 & FileName) <= Time2 Then
FindFileTime = FileDateTime(Path1 & FileName)
FindFileName = FileName
FindFileByPathBeTweenTime = FileName
Exit Function
End If
FileName = Dir
Wend
End Function
'[简介]:
'查找目录下时间介于2个时间之间的一个文件
Function FindFileByPathBeTweenTime(Path1 As String, Time1 As Date, Time2 As Date, Optional FindFileName As String, Optional FindFileTime As Date) As String
'[mycode_id:2067],edittime:2011-9-30 下午 08:26:44
'Path1要求\字符结尾
Dim FileName As String
FileName = Dir(Path1)
While FileName <> ""
If FileDateTime(Path1 & FileName) >= Time1 And FileDateTime(Path1 & FileName) <= Time2 Then
FindFileTime = FileDateTime(Path1 & FileName)
FindFileName = FileName
FindFileByPathBeTweenTime = FileName
Exit Function
End If
FileName = Dir
Wend
End Function
'┗〓〓〓〓〓〓〓〓〓 FindFileByPathBeTweenTime,end 〓〓〓〓〓〓〓〓〓┛