File System Object(FSO对象)B

一、实例FSO获取当前路径下的文件

 1 Sub Fsotest()
 2     Dim Fso As New FileSystemObject, Path As String, File
 3     Path = ThisWorkbook.Path & "\"
 4     With Fso
 5         For Each File In .GetFolder(Path).Files '遍历路径下的所有Files
 6             If File.Name <> ThisWorkbook.Name Then
 7                 Debug.Print File.Name
 8             End If
 9         Next
10     End With
11 End Sub

 二、实例FSO遍历当前文件夹所有子文件夹

 1 Sub test()
 2     Call Getfd(ThisWorkbook.Path & "\")
 3 End Sub
 4 
 5 Sub Getfd(ByVal Path As String)
 6     Dim Fso As New FileSystemObject
 7     Dim Folder As Variant
 8     For Each Folder In Fso.GetFolder(Path).SubFolders '遍历文件夹
 9         Debug.Print Folder
10         Getfd (Folder) '递归遍历子文件夹
11     Next
12 End Sub

 三、实例FSO遍历当前文件夹及子文件夹下的所有Excel文件

 1 Sub Test()
 2     Call GetFile(ThisWorkbook.Path & "\")
 3 End Sub
 4 
 5 Sub GetFile(ByVal Path As String)
 6     Dim Fso As New FileSystemObject
 7     Dim Folder As Variant, File As Variant
 8     
 9     For Each File In Fso.GetFolder(Path).Files '遍历 path路径下文件
10         If File.Name Like "*.xls*" Then
11             Debug.Print Path & File
12         End If
13     Next
14     
15     For Each Folder In Fso.GetFolder(Path).SubFolders '遍历文件夹
16         'Debug.Print Folder
17         GetFile (Folder) '递归遍历子文件夹
18     Next
19 End Sub

 

posted @ 2019-01-11 16:03  易塞尔工作室  阅读(487)  评论(0编辑  收藏  举报