下面是一個使用遞歸調用從FileSystemObject()示例拼湊在一起的方法。如果需要,對結果應用排序。您也可以通過使用其他FileSystemObject的.txt擴展過濾()方法:
Sub Sample()
ShowFolderList ("C:\temp")
End Sub
Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s, sFldr
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 In fc
If Right(f1, 1) <> "\" Then ShowFolderList f1 & "\" Else ShowFolderList f1
Next
Set fc = f.Files
For Each f1 In fc
Debug.Print folderspec & f1.Name
Next
End Sub
寫入文件:
Option Explicit
Dim file As Object
Dim fs As Object
Sub go()
Set fs = CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile("C:\temp2\results3.txt", 2, True) ' 2=ForWriting, replace
ShowFolderList "C:\temp\"
file.Close
MsgBox "done"
End Sub
Sub ShowFolderList(folderspec)
On Error GoTo local_err
Dim f, f1, fc, s, sFldr
Set f = fs.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 In fc
If Right(f1, 1) <> "\" Then ShowFolderList f1 & "\" Else ShowFolderList f1
Next
Set fc = f.Files
For Each f1 In fc
file.writeline folderspec & f1.Name
Next
local_exit:
Exit Sub
local_err:
MsgBox Err & " " & Err.Description
Resume local_exit
Resume
End Sub
http://stackoverflow.com/questions/9827715/get-list-of- subirrs-in-vba –
將Dir(「C:\ Work \ NCL \ nCLs \ histogram_addition \ TestData \ Input \ RTE \」)改爲'Dir(Range(「A1」)。Value)'(修改爲正確的這個代碼將結果打印在VBE的Immediate窗格中,你可以修改它來創建一個文本文件並將每個路徑打印到文件中的一個新行 –
我是VBA新手,剛剛檢查了直接窗格並且它的空洞,任何想法爲什麼? – manish449