1. 程式人生 > >列出檔案目錄結構

列出檔案目錄結構

以下程式碼可快速列出所有資料夾,子資料夾下檔案

用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