'获取某目录下的所有子目录及文件的名称 Public Sub SeachFile(ByVal strPath As String) Dim Fso As Object Dim Fol As Object Dim Fil As Object Set Fso = CreateObject("Scripting.FileSystemObject") Set Fol = Fso.GetFolder(strPath)
For Each Fil In Fol.Files Debug.Print Fil.Path '打印路径及文件名 Debug.Print Fil.Name '只打印文件名 Next For Each Fol In Fol.subfolders Debug.Print Fol.Path '打印子目录的名称 Debug.Print Fol.Name '只打印目录名 SeachFile Fol Next End SubPrivate Sub Command1_Click() SeachFile ("C:\Test\") End Sub
Public Sub SeachFile(ByVal strPath As String)
Dim Fso As Object
Dim Fol As Object
Dim Fil As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fol = Fso.GetFolder(strPath)
For Each Fil In Fol.Files
Debug.Print Fil.Path '打印路径及文件名
Debug.Print Fil.Name '只打印文件名
Next
For Each Fol In Fol.subfolders
Debug.Print Fol.Path '打印子目录的名称
Debug.Print Fol.Name '只打印目录名
SeachFile Fol
Next
End SubPrivate Sub Command1_Click()
SeachFile ("C:\Test\")
End Sub