以下代码可快速列出所有文件夹,子文件夹下文件
用tree /f显示为树形结构
mypath = wsh.exec("cmd /c tree /f " & Chr(34) & mypath & Chr(34)).StdOut.ReadAll
用 dir显示为长文件名结构
mypath = wsh.exec("cmd /c dir /a /s /b /a-d " & Chr(34) & mypath & Chr(34)).StdOut.ReadAll
'http://club.excelhome.net/thread-967698-2-1.html
Sub File_List()
On Error GoTo 100
Dim wsh As Object, mypath As String, ar, st$
mypath = "C:\Users\zhengh\Documents\BARTEC"
Set wsh = CreateObject("wscript.shell")
mypath = wsh.exec("cmd /c tree /f " & Chr(34) & mypath & Chr(34)).StdOut.ReadAll
mypath = Left(mypath, Len(mypath) - 1)
ar = Split(mypath, vbCrLf)
ar = Filter(ar, st)
Set wsh = Nothing
Cells.ClearContents
Range("a1").Resize(UBound(ar) + 1) = Application.Transpose(ar)
100:
End Sub