Below is some recursive code that should get you going. It lists every
directory, then files in that directory, followed by all the
subdirectories, their files and folders, and so on as deep as the
nested file structure goes. The code is based on a technique called
is used to loop through all the subfolders and their subfolders, and
so on. DoOneFolder deals with a single folder and then calls itself of
each subfolder, which is handled by DoOneFolder, which will in turn
call itself for all the subfolder of the current folder. The code
does not have all the bells and whistles of the Folder Tree add-in I
have at http://www.cpearson.com/excel/FolderTree.aspx but it should
get you going in the right direction.
Sub FolderListing()
' requires reference to Microsoft Scripting Runtime
Dim StartFolderName As String
Dim FSO As Scripting.FileSystemObject
Dim StartF As Scripting.Folder
Dim F As Scripting.File
Dim FF As Scripting.Folder
Dim R As Range
Set R = Worksheets("Sheet1").Range("A1")
StartFolderName = "C:\AlerterNET2" '<<<< CHANGE
Set FSO = New Scripting.FileSystemObject
Set StartF = FSO.GetFolder(StartFolderName)
R.Value = StartF.Path
Set R = R(2, 1)
For Each F In StartF.Files
R.Value = F.Name
Set R = R(2, 1)
Next F
For Each FF In StartF.SubFolders
DoOneFolder FF, R, FSO
Next FF
End Sub
Sub DoOneFolder(FF As Scripting.Folder, R As Range, FSO As
Scripting.FileSystemObject)
Dim F As Scripting.File
Dim SubF As Scripting.Folder
R.Value = FF.Path
Set R = R(2, 1)
For Each F In FF.Files
R(1, 2).Value = F.Name
Set R = R(2, 1)
Next F
For Each SubF In FF.SubFolders
DoOneFolder SubF, R, FSO
Next SubF
End Sub
Change the line marked with "<<<<" to the name of the folder in which
the listing should start.
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]