Excel - Macro: make list of all files in all folder with sub folders

Asked By Snoopy on 14-Dec-09 04:25 AM
Hey guys
You have kindly helped me before, and I hope for a little bit more
My macro (below) makes a filename-list of all FILES in one certain
(SUB)FOLDER, but I need to redesign it to make a list of ALL FILES in
FOLDER + ALL FILES in every SUBFOLDER (on every sublevel) in this

I think you guys know the trick - will you share it with me?
Best regards

My macro goes like this:
(How can I modify it?)

Sub ListAllFilesInFolderWithDir()
' Delete existing list in sheet FILES/column B to prepare for new
On Error Resume Next

' Search FOLDERS named in celle D1, and make a folder list (Listbox
used for pick one of these for the next move)
Dim WorkFile As String
Dim i As Integer

Path = Range("D1").Value
i = 1
WorkFile = Dir(Path & "*.*")
Do While WorkFile <> ""
Cells(i, 2).Value = WorkFile
WorkFile = Dir()
i = i + 1

' When picked a subfolder in listbox: make a filename-list og files in
this subfolder (named in celle D2)
If Range("D2").Value <> "" Then
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
End If

' Finally copy/paste the list into the rihgt position
Set tbl = ActiveCell.CurrentRegion
tbl.Offset(1, 1).Resize(tbl.Rows.Count - 1, tbl.Columns.Count -
Selection.PasteSpecial Paste:=xlPasteFormulas,
Operation:=xlNone, SkipBlanks _
With Selection.Font
End With
End Sub

Pete_UK replied to Snoopy on 14-Dec-09 05:57 AM
Chip Pearson shows how this can be done:


Perhaps you can get some tips from there (or even download his

Hope this helps.


Snoopy replied to Pete_UK on 14-Dec-09 09:08 AM

Sorry Pete
I did not quite help me
I manage to list filenames in one-level subfolder, but nott ALL files
in sub- and sub-sub-folders and so on as well - e.g. all FILES
together in one folder with all underlying sub folders.

Best regards
(still howling at the moon....)
Pete_UK replied to Snoopy on 14-Dec-09 09:08 AM
Okay, well try this link:


Hope this helps.

Chip Pearson replied to Snoopy on 15-Dec-09 04:30 PM
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
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.

Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
[email on web site]
Nick Vivian replied to Snoopy on 16-Dec-09 07:16 AM
Try this:
Jason Taylor replied to Chip Pearson on 05-Aug-10 02:39 AM
Excellent code Chip - thanks.  The only thing missing for my particular application is adding the date of the file as listed in the file tree (I assume that is the date last modified).  Any guidance you can provide to add this information would be much appreciated!