Site icon Professor Excel

List of all Files and Folders: How to Easily Insert a Directory in Excel!

You probably don’t need this every day: But once a file you might want to have a list of all files within a folder or directory in Excel. The good thing: There are many methods available. If you Google it, you will find a lot of different methods to create a file list in Excel. But unfortunately, all of them have advantages and disadvantages – which you usually only find out after trying them. That’s the reason for this article: You won’t only learn each method step by step: You will rather find an overview comparing all the advantages and disadvantages of each method. After seeing this, you don’t need to start extensive trials and errors. Much better: You can easily select the method that works best for you!

Summary

Method 1:
Copy & Paste on Mac
Method 2:
Named function
Method 3:
Internet browser
Method 4:
PowerQuery
Method 5:
Excel-Add-In
Method 6:
Windows PowerShell
Method 7:
VBA Macro
Short descriptionCopy and paste from the Mac Finder.Use a function in a named range to insert a file list.Open folder with a web browser and copy the file list.Use PowerQuery to insert a directory.Let an Excel add-in do the work for you.Use the Windows built-in PowerShell feature.Use a VBA Macro to insert a file list.
Ease of use
Operating systemMacWindowsWindows / MacWindows / MacWindowsWindowsWindows
Include subfolders
and files
NoNoNoYesYesYes(Yes) (depends on the VBA macro)
File information availableFile namesFile namesFile names, file links (not working properly), file size, date modifiedFolder path, name
extension, date accessed, date modified, date created

Content type, kind, size, ReadOnly, hidden, S
system, directory, archive, device, normal, temporary, SparseFile, ReparsePoint, compressed, offline, NotContentIndexed, encrypted, ChangeTime, SymbolicLink, MountPoint
Links to files on the drive, file path, date and time last saved, date and time created, file size, file typeFile name, file path, date and time last saved, file sizeDepends on the VBA macro
TechnologyMac FinderNamed function in ExcelInternet browserPowerQueryExcel Add-InPowerShell, Text Import WizardVBA
CommentFast but with limited options.Only shows files, no folders.File links not working in our test; not working with all internet browsers (tested with Google Chrome and working).Elegant and uses built-in functions.Convenient, fast and many options.Results not good, difficult process; performs well on large file structures, though.Modifying the macro rather for advanced users.
LinkClick hereClick hereClick hereClick hereClick hereClick hereClick here

Method 1: Simply copy and paste from Mac Finder to Excel

File list on a Mac: Just copy and paste from Finder to Excel.

This first method works on a Mac only: Just select all files in a Finder window and press copy (Command + C on the keyboard). Next, switch to Excel and paste the list: Press Command + V on the keyboard.

The advantage of this method is that it’s very easy and fast. Unfortunately, you don’t have any further options than just pasting the file names. That means you can’t insert data of subfolders or file properties.

Recommendation: Use this if you need a quick file list on the Mac – without any further information.

Method 2: Insert a file list with built-in Excel functions using “Named Ranges”

The second method is actually quite elegant: Use built-in Excel functions to insert a file list in Excel. It is built on advanced functions, such as an Excel function within a named range. But following these steps should be quite straight-forward.

Set up the named range

Create a list of all files with Excel built-in features. Here: A function within a named range.
  1. Choose a cell in which you later write the folder path. In this case, it’s cell C4.
  2. Click on “Defined Name” on the Formulas ribbon.
  3. Give a name for the cell containing the folder path (here: “Folder_Path”)
  4. In the “Refers to” field, type: =files(linktopathcell) (replace “linktopathcell” with your cell reference – in this case “Method_2!$C$4”).
  5. Confirm with OK.

Use the named range in Excel functions

Copy the folder path from Windows Explorer.
  1. Go to the Windows Explorer and copy the folder path.
As the last step, set up a INDEX formula that pulls the file names into cells.
  1. Paste the folder link into the folder path cell (the cell you have set as the named range in step 1 above). Add one of the following endings:
    • If you want to include all files – no matter which file type – in your list, add \*
    • For listing only Excel files, add \*.xls*
    • If you want to see all files ending on “.xlsx”, add \*.xlsx
  2. As the last step, enter an INDEX function for compiling the list (here in cell B7):
    =INDEX(Folder_Path,ROW(B1))
    Folder_Path should be the same name that you have given in step 3 above. The ROW function should refer to any cell in the first row (for example to B1). That means, this argument could also be A1, C1, etc.
    As the last step: Copy the INDEX function down until you see the first #REF error. #REF means in this case that there are no more files in your folder. If you like, you could wrap the IFERROR function around in order to mitigate the error.

Recommendation: Use this if you only need a list of files (no subfolders) that dynamically updates itself.

Method 3: Use an internet browser to quickly copy and paste a file list to Excel

I must say, I like this idea because it’s almost as fast as our method number 1 above: Open a folder in a webbrowser and then copy & paste the list to Excel.

Copy the folder path from Windows Explorer.
  1. Copy the folder path from the Windows Explorer like on the screenshot or Mac Finder (for Mac: open file Info (right-click on folder and then on “Get Info”. Select and copy link from “Where” section).
Open the folder path with a web browser. Then copy and paste the file list into Excel.
  1. Open a browser, for example Google Chrome (should be working with most other browsers as well). Paste the previously copied link into the address field.
  2. Copy the table of files.
  3. Open Excel and paste the file list.

One comment to this methods: The links (which are also created) usually won’t work in Excel.

Recommendation: Use this if you quickly need a file list (without subfolders), including some file properties such as file size and date last saved.

Method 4: Create a file list with PowerQuery

PowerQuery can also create lists of all files and folders, including subfolders.

The next method is also among my favorites: It uses PowerQuery and comes with the advantages that it doesn’t require any complex programming or third-party technologies. Also, it can be refreshed easily later on.

  1. Go to the Data ribbon, click on “Get Data” on the left, and select “From Folder” in the “From File” sub-menu.
  2. Select the Folder and click on “Open” (not in the screenshot on the right).
  3. You can now see a preview. Click on “Transform Data”.
Besides that, PowerQuery allows to insert various file attributes.
  1. Remove the first column “Content” (right-click on the heading and click on “Remove”).
  2. If you want to see more file attributes than shown already in the preview, click on the small arrows of the “Attributes” column and select the attributes.
  3. Click on Close & Load.

Recommendation: Use this method if you to have lots of file properties. It can later on easily be refreshed.

Method 5: Excel Add-in Professor Excel Tools

The Excel add-in Professor Excel Tools offers a function to easily insert directories. It regards subfolders and can insert various file properties:

The Excel add-in Professor Excel Tools provides a function to insert a directory in Excel.

You can further specify, where the directory should be created.

Using the “Get Directory” function is very simple:

  1. Click on the Directory button on the Professor Excel ribbon.
  2. Select all the file properties you’d like to show and the location, where the directory table should be placed in your Excel file.
  3. Click on “Select folder and start”. Then, choose the folder and proceed with ok.

That’s it. Please feel free to download Professor Excel Tools here.

Recommendation: Use this method if you accept third-party add-ins within Excel. If you do, it’s quite convenient and fast.


This function is included in our Excel Add-In ‘Professor Excel Tools’

(No sign-up, download starts directly)

More than 35,000 users can’t be wrong.


Method 6: Use PowerShell to extract a file list

As you can see in the following description, using PowerShell takes quite a lot of steps. It might be useful, though, if you have very large folder structures. Otherwise, try to use a different method.

Open PowerShell and write the directory into a text file

The PowerShell method is very complex. First open the folder in PowerShell and paste a line of code.
  1. Navigate to the folder which you want to have the file list from. Hold down the Shift key on the keyboard and right-click on it (if you don’t hold down the Shift key, the option of “Open PowerShell window here” is not available).
  2. Click on “Open PowerShell window here”.
  3. Copy and paste the following code and press enter afterwards:

Get-ChildItem -Path $MyInvocation.MyCommand.Path -force -recurse > filelist.txt

Import text file into Excel

Open Microsoft Excel.

Next, import a text file containing the directory into Excel.
  1. Open the Text Import Wizard: Go to the Data ribbon, click on “Get Data” on the left, then on “Legacy Wizards” and then on “From Text (Legacy)”. If this option is not available, you can activate it within the Excel settings (check this article for more information).
  2. Select the file filelist.txt from your folder. In step 3 above it was created within your target folder.
Fine-tune the import so that all files are shown correctly.

Follow the import steps of the Text Import Wizard on the screen:

  1. In the first step of three, make sure that “Fixed width” is selected. Click on Next.
  2. In the lower part of the window, Excel shows a preview of your import. Check here, if the column separators are set correctly: You can move the vertical lines if necessary or add more by clicking on the respective number in the heading of the preview. Then click on Next and in the third step Next again. After that finish the Text Import Wizard by selecting the location where the file list should be placed.
  3. As you can see in number 8, the file list is inserted, but has some disadvantages: Directory paths are cut, each subfolder has its own block, and so on.

In a nutshell: This method of creating file lists within Microsoft Excel via PowerShell is complex and the results aren’t as good as in the other methods.

Recommendation: Try to avoid this method.


Do you want to boost your productivity in Excel?

Get the Professor Excel ribbon!

Add more than 120 great features to Excel!


Method 7: Let a VBA Macro loop through the files

The following VBA code creates a list of all files in a folder. The list will be placed on the currently active worksheet, starting from cell A1. Please make sure that you don’t override anything here.

Sub InsertFileList()

Dim objFileSystemObject As Object, objFolder As Object, objFile As Object, i As Integer

Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFileSystemObject.GetFolder("Your folder path")

For Each objFile In objFolder.Files
    i = i + 1
    ActiveSheet.Cells(i, 1) = objFile.Name
Next

End Sub
Insert a new module in VBA.

Here is how to use the VBA-Code. If you need more help of how to use VBA macros, please refer to this article.

  1. Copy the VBA code from above.
  2. Open the VBA editor by pressing Alt + F11 on the keyboard.
  3. Insert a new module: Right-click on the active workbook name on the left.
  4. Go to “Insert” and click on “Module”.
Paste the code sample from above, replace “Your folder path” and run the macro.
  1. Paste the code by pressing Ctrl + V on the keyboard.
  2. Replace Your folder path with the path from your own folder.
  3. Click on start in the VBA editor.

Download

Please feel free to download all examples from above in this comprehensive Excel workbook. Each method is located on a different worksheet. Just click here and the download starts.

Image by Pexels from Pixabay

Exit mobile version