一个参考例子:Public Function File_Folder_List(ByVal source As String, Optional dFileList As Variant = Null, Optional dFolderList As Variant = Null) As Boolean '循环处理文件集合 Dim fs, f, f1, d1, bFlag As Boolean On Error GoTo lError bFlag = True Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(source) 'fs 有 Files 和 SubFolders 集合
'没有参数时,若要使 dFileList dFolderList 参与循环,将以下 If 语句封住即可 If Not IsNull(dFileList) Then Set dFileList = f.Files For Each f1 In dFileList With f1 '处理集合中的文件
' End With Next f1 End If If Not IsNull(dFolderList) Then Set dFolderList = f.SubFolders For Each d1 In dFolderList With d1 '处理集合中的文件夹
' End With Next d1 End If GoTo lExit lError: 'MsgBox: vbCritical vbExclamation vbInformation vbQuestion MsgBox "文件夹 " & source & " 有误,请检查!", vbOKOnly + vbExclamation, "警告" bFlag = False lExit: If IsObject(f1) Then Set fd = Nothing If IsObject(d1) Then Set fc = Nothing If IsObject(f) Then Set f = Nothing If IsObject(fs) Then Set fs = Nothing End Function
1、使用Dir查找符合条件的文件
2、使用FileCopy复制文件大目标文件夹
'循环处理文件集合
Dim fs, f, f1, d1, bFlag As Boolean
On Error GoTo lError
bFlag = True
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(source) 'fs 有 Files 和 SubFolders 集合
'.Count .Item(key) [= newitem]
'.Name .Path .ShortName .ShortPath .Size .Type .Attributes .DateCreated .DateLastAccessed .DateLastModified .ParentFolder
'.Drive(AvailableSpace .DriveLetter .DriveType .FileSystem .FreeSpace .IsReady .Path .TotalSize, .RootFolder
'.SubFolders{}
'没有参数时,若要使 dFileList dFolderList 参与循环,将以下 If 语句封住即可
If Not IsNull(dFileList) Then
Set dFileList = f.Files
For Each f1 In dFileList
With f1
'处理集合中的文件
'
End With
Next f1
End If If Not IsNull(dFolderList) Then
Set dFolderList = f.SubFolders
For Each d1 In dFolderList
With d1
'处理集合中的文件夹
'
End With
Next d1
End If
GoTo lExit
lError:
'MsgBox: vbCritical vbExclamation vbInformation vbQuestion
MsgBox "文件夹 " & source & " 有误,请检查!", vbOKOnly + vbExclamation, "警告"
bFlag = False
lExit:
If IsObject(f1) Then Set fd = Nothing
If IsObject(d1) Then Set fc = Nothing
If IsObject(f) Then Set f = Nothing
If IsObject(fs) Then Set fs = Nothing
End Function