Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to list all files in folder and subfolders into a worksheet?

Have you ever tried to list all filenames from a folder into a worksheet including the files located within its subfolders? In fact, there is no direct way for us to list the filenames from a folder and its subfolder in Excel, however, today, I will introduce some quick tricks to solve this job.

List all filenames in folder and subfolder with VBA code

List all filenames in folder and subfolder quickly and easily with Kutools for Excel


List all files from a folder including subfolders into a worksheet:

Kutools for Excel's Filename List feature, you can quickly list all file names from a folder including subfolders into a new worksheet. Click to download and free trial Kutools for Excel now!

doc list files in folder subfolder 9

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!



Normally, Excel has no build in feature to deal with this task, but, you can apply the following VBA code to complete this problem.

1. Activate a new worksheet which will list the filenames.

2. Hold down the ALT + F11 keys in Excel, and it opens the Microsoft Visual Basic for Applications window.

3. Click Insert > Module, and paste the following code in the Module Window.

VBA code: List all filenames in folder and subfolder

Sub MainList()
'Updateby20150706
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
Call ListFilesInFolder(xDir, True)
End Sub
Sub ListFilesInFolder(ByVal xFolderName As String, ByVal xIsSubfolders As Boolean)
Dim xFileSystemObject As Object
Dim xFolder As Object
Dim xSubFolder As Object
Dim xFile As Object
Dim rowIndex As Long
Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFileSystemObject.GetFolder(xFolderName)
rowIndex = Application.ActiveSheet.Range("A65536").End(xlUp).Row + 1
For Each xFile In xFolder.Files
  Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
  rowIndex = rowIndex + 1
Next xFile
If xIsSubfolders Then
  For Each xSubFolder In xFolder.SubFolders
    ListFilesInFolder xSubFolder.Path, True
  Next xSubFolder
End If
Set xFile = Nothing
Set xFolder = Nothing
Set xFileSystemObject = Nothing
End Sub
Function GetFileOwner(ByVal xPath As String, ByVal xName As String)
Dim xFolder As Object
Dim xFolderItem As Object
Dim xShell As Object
xName = StrConv(xName, vbUnicode)
xPath = StrConv(xPath, vbUnicode)
Set xShell = CreateObject("Shell.Application")
Set xFolder = xShell.Namespace(StrConv(xPath, vbFromUnicode))
If Not xFolder Is Nothing Then
  Set xFolderItem = xFolder.ParseName(StrConv(xName, vbFromUnicode))
End If
If Not xFolderItem Is Nothing Then
  GetFileOwner = xFolder.GetDetailsOf(xFolderItem, 8)
Else
  GetFileOwner = ""
End If
Set xShell = Nothing
Set xFolder = Nothing
Set xFolderItem = Nothing
End Function

4. After pasting the code into the Module, press F5 key to run this code, and a Macros dialog box pops up, select the MainList macro name, and then click Run button, see screenshot:

doc list files in folder subfolder 1

5. And in the Browse window, please select the folder that you want to list all filenames including the subfolders, see screenshot:

doc list files in folder subfolder 2

6. After specifying the folder, then click OK button, and all the filenames in the folder and its subfolders have been listed into current worksheet from cell A2, see screenshots:

doc list files in folder subfolder 3
 1
doc list files in folder subfolder 4

With above code, you can just list the filenames, sometimes, you need to list other attributes, such as file size, file type, created time, containing folder and so. Kutools for Excel contains a useful function – Filename List, with this feature, you can quickly list all or specific types of filenames in a folder and its subfolders.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days.

After installing Kutools for Excel, please do with following steps:

1. Click Enterprise > Import / Export > Filename List, see screenshot:

doc list files in folder subfolder 5 5

doc list files in folder subfolder 6 6

2. In the Filename List dialog box, do the following operations:

A: Click doc list files in folder subfolder 7button to choose the folder that you want to list the filenames;

B: Specify the file type that you want to list from the Files type section;

C: Select one file size unit you want to display from the File size unit section.

Note: To list the filenames from the subfolder, please check Include files in subdirectories, if you check Create hyperlinks option, it will create hyperlinks for each filenames and folders.

 Download and free trial Now !

3. Then click OK button, all of the files contained in the selected folder and its subfolders have been displayed with following attributes in a new worksheet. See screenshot:

doc list files in folder subfolder 8

Click to know more details about this Filename List utility.

Download and free trial Kutools for Excel Now !


Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    James · 2 months ago
    My VBA is saying that the variable folder is not defined. Anyone know why this is?
  • To post as a guest, your comment is unpublished.
    Chris K · 7 months ago
    What was the purpose of the parameter ByVal xIsSubfolders As Boolean?
  • To post as a guest, your comment is unpublished.
    Leandro Barbosa · 7 months ago
    Extremamente elegante este código!
  • To post as a guest, your comment is unpublished.
    jumpjack · 9 months ago
    Sub "GetFileOwner()" in code above is not used.
    • To post as a guest, your comment is unpublished.
      skyyang · 9 months ago
      Hello, jumpjack,
      The above code works well in my Excel, which Excel version do you use?
  • To post as a guest, your comment is unpublished.
    Brett · 1 years ago
    Thankyou for this code it has helped me with a request a work, I am now able to import these results into Access for further transformation.Appreciate it.