21. VBA Tips - List Files In A Folder

This VBA tip will demonstrate how to list all the files in a folder.
- We'll learn about Scripting Objects
- We'll also see how to recursively call the sub routine so that we can also list files in the subfolders
- The example I will use is the case of trying to manage your photos
- You have thousands of photos all over the place and you think you have replicas that you might be able to delete to save disk space
Here's an example folder, it's contents, and it's subfolder's contents.
Here's what we want to achieve in Excel:
Here's the code we will use:
Explanation
- Open the practise sheet, and recreate the folders if you want to follow along with the tip
- We start by dimming iRow
- The reason for this is so that we can share it between the subroutines, and when we loop through the subfolders, it continues to increment
- iRow is the row that we will put the file name / path etc..
- The ListFiles Sub routine sets the start row... in our case we want row 11
- It also calls the ListMyFiles procedure with inputs equal to the values you input on the sheet. (B7 and B8)
- You can change that path (B7) to whatever you want
- You can also change whether to include or exclude subfolders (B8) by changing from TRUE to FALSE
- The ListMyFiles procedure then creates a FileSystemObject (myObject), and Source folder object (mySource)
- We then have the line: On Error Resume Next
- This was included so that the code doesn't break if you're looking at a folder you don't have access to
- We then loop through each of the files in the source folder, get the attributes we're interested in, and output them to our sheet.
- The last bit of the code asks: if we want to include SubFolders, then call ListMyFiles with the subfolder as input. This ensures that cannot exit the code until all the subfolders are done.
The output we get on the sheet tells us that there are several files that have the same name, last modified date, and size... so we should have a look and see whether we can delete one of them and free up disk space.
Download sheet to practise how to List Files In A Folder in Excel
Training Video on how to List Files In A Folder in Excel:
| Attachment | Size |
|---|---|
| list-files-in-a-folder.xls | 850.5 KB |
»
- Nick's blog
- Add new comment
- 2932 reads

won't work
I get user-defined errors around line 10, column 50.
Thank you
That works nicely.
How to modify to only show one or two type(s) of file(s)
How would I modify your code to only show 1 or 2 types of files.
I found one bit of code that will allow me to set the type of file that will be shown with the following code:
Dim filetype As String
filetype = "dwg"
fName = Dir(fPath & "*." & filetype)
While fName <> ""
i = i + 1
ReDim Preserve fileList(1 To i)
fileList(i) = fName
fName = Dir()
Wend If
i = 0 Then
MsgBox "No files found"
Exit Sub
End If
For i = 1 To UBound(fileList)
ws.Range("A" & i + startrow).Value = fileList(i)
Next
However I'm having a hard time adding that to your code. Any help that you could offer would be great. Thanks, Michael
filter
run it as is and use autofilter to show what you want
alternatively, use instr on the file name so see if it contains ".txt" for text files... etc.
p.s. adding your email address will lead to a tonne of spam..
If you're not a member, Register here
Thanks
I don't know why I did not think of that.
Thanks
It works fine for me.
I got this excel at right time. I Appreciate your work.
My suggestion is Please add one more button to clear the data After use.
Good Luck.
Regards,
A.Kalusalingam
Thank you
This was very useful and was one of the cleanest and most concise examples on how to do a recursive directory listing that I have found
Well done and Thanks a lot for the accompanying video/sheet
Pri