有两种方法:
1、使用DIR递归,不过这是微软明确不推荐的
2、最快但不太正规的
使用 Shell "cmd /k dir c:\windows /s/b/a >>d:\jg.txt"
c:\windows 目标文件夹,当前文件夹为 app.path,你可以根据要求生成命令字符串
d:\jg.txt 为临时文件
执行这行后再读取临时文件即可。极快!
先引用“Microsoft Scripting Runtime”
Option Explicit
Private fso, objFolder, Folder, txt, DeskTop, Drive, wsh
Private Sub Command1_Click()
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set wsh = CreateObject("Wscript.Shell")
DeskTop = wsh.SpecialFolders("desktop")
Set txt = fso.CreateTextFile(DeskTop & "\AllFolders.txt")
For Each Drive In fso.Drives
Bianli (Drive.RootFolder)
Next
MsgBox "文件夹遍历完毕!"
txt.Close
Set txt = Nothing
Set fso = Nothing
End Sub
Private Sub Bianli(Path As String)
Set Folder = fso.GetFolder(Path)
For Each objFolder In Folder.SubFolders
txt.WriteLine objFolder.Path
Next
For Each objFolder In Folder.SubFolders
Bianli (objFolder.Path & "\")
Next
End Sub
路径保存在桌面上的 AllFolder.txt
app.path 当前路径