List File Names in a Folder in Excel

Mynda Treacy

June 4, 2024

Ever heard of the Excel FILES function? If you haven't, you're not alone.

This little-known function can list all file names in a folder and has been part of Excel for many versions. However, it's hidden as a legacy Excel 4 Macro function.

In this post I'll reveal how to use the FILES function and another way to list all file names in a folder without a formula and explore the various applications.


List All File Names in a Folder Video

Subscribe YouTube

 

Download Workbook

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

Getting Started with the Excel FILES Function

The FILES function is a legacy Excel 4 Macro function that lists all files contained within a specified folder.

Though not available in the conventional Excel functions list, you can still use it if you know how to enable it.

Setting Up the FILES Function

To demonstrate, let's create a list of files in a folder on my C:\ drive.

Note: You need to add a backslash and an asterisk \* at the end of the folder path for the function to work, like so:

C:\temp\Demo\*

To enable the FILES Function:

  1. Define the FILES Function as a Named Formula:

Go to the Formulas tab and click on Define Name:

Define Name

Name it fileList

In the Refers to section, input the formula: =FILES(Sheet1!$C$3) where $C$3 contains your folder path.

Refers to section

  1. Using the FILES Function in Excel:

In versions of Excel that support dynamic arrays, simply type =fileList to spill the results across multiple columns.

For a column list, wrap it in the TRANSPOSE function:

TRANSPOSE Function

If your Excel version doesn't support dynamic arrays, there are alternative methods available, which I'll discuss later.

Dynamic Folder Path

To dynamically update the folder path based on the location of your file:

  1. Use the CELL function with TEXTBEFORE to return the file path of the current file and concatenate it with an asterisk:

Dynamic Folder Path

Note: If you're using an earlier Excel version that doesn't support TEXTBEFORE, use this formula:

=LEFT(CELL("filename",C1),FIND("[",CELL("filename",C1))-1)&"*"

Filtering File Types

To filter the list to specific file types, modify the path to include the file extension, for example: C:\temp\Demo\*.xlsx for Excel files only.

For more dynamic filtering:

  1. Create a dropdown list with common file types and an option to show all file types.

Dropdown List

  1. Use the IF function to check the file type selected in a cell (e.g. C5):

IF(C5="all files", "", C5)

Append this formula to the folder path with the ampersand for dynamic filtering.

Dynamic Filtering

Counting Files

To count the files in a folder, wrap the fileList formula in the COUNTA function:

=COUNTA(fileList)

COUNTA

Non-Dynamic Arrays Formula to List Files in a Folder

For users on Excel 2019 and earlier you can use the INDEX function to extract file names and wrap it in the IFERROR function to allow copying it down more rows than you currently have files for in the folder. This will automatically list any new files added to the folder.

  1. I'll define a new FILES formula for non-dynamic arrays (e.g. fileListNonDA):

Non-Dynamic Arrays Formula

  1. Extract File Names with INDEX and IFERROR:

=IFERROR(INDEX(fileListNonDA, ROW(A1)), "")

Copy the formula down to cover the expected number of files and allow for more files if required:

Extract File Names

Generating a Hyperlinked List of Files

If you want to create a clickable list of file names that open the files directly from Excel, we can use the non-dynamic array technique above to extract the list of files in a folder and hyperlink them:

  1. Create Hyperlinks:

Modify the formula above to include the HYPERLINK function with the file path and file name as follows:

=IFERROR(HYPERLINK(LEFT($C$3, LEN($C$3)-1) & INDEX(fileListNonDA, ROW(A1)), INDEX(fileListNonDA, ROW(A1))), "")

  1. Apply Conditional Formatting:

The hyperlinks generated with a formula like this don't get automatically formatted in blue font with an underline, however we can use Conditional Formatting to do this for us.

Go to the Home tab > Conditional Formatting > New Rule:

Conditional Formatting

Select the 'Use a formula to determine which cells to format' and in the formula field type:

=$C6<>""

Then click the 'Format…' button:

Format Cells

In the Format Cells dialog box apply a blue font and underline:

Format Cells Dialog Box

Enabling Macros

Since the FILES function is a macro, you need to save your file as .xlsm and enable XLM macros in Excel. Here's how:

  1. Go to the File tab, click Options > Trust Center > Trust Center Settings.
  2. Under Macro Settings, select Enable Excel 4.0 macros when VBA macros are enabled:
  3. Enabling Macro

  4. Adjust File Block Settings to allow opening Excel 4 Macrosheets in Protected View:

File Block Settings

Using Power Query as an Alternative

For an alternate method that works in all versions of Excel and doesn't require macros, use Power Query to list file names in a folder and sub-folders:

  1. Import File Names with Power Query:

Go to the Data tab > Get Data > From File > From Folder:

Power Query Get Data

Browse to your folder or paste the path.

Paste the Path

  1. Transform and Load Data:

In the Power Query editor, click on the double headed arrow on the Attributes column and select the attributes you want (e.g., file size):

Select the attribute

Filter out any folders you don't want the file names for.

Rename the query, remove unnecessary columns, and load the data.

Filter Unwanted Folders

  1. Filter and Refresh:

Filter the list based on file extensions.

Filter based on extensions

Or insert a Slicer for easy filtering and refresh to update the list with new files.

Refresh to update

Choose the field you want a Slicer for:

Select a field in the Slicer

Select the file extension you want to display in the list:

Select the file extension

Conclusion

The FILES function is a hidden gem in Excel, offering a powerful way to manage file lists. Whether you use the legacy macro approach or Power Query, you now have the tools to list file names in a folder in Excel effectively.

8 thoughts on “List File Names in a Folder in Excel”

  1. Hi Susan,

    I am using the macOS version of Excel.
    Unfortunately, none of the options you explain in this page is available in the macOS edition. I don’t understand why the folder is not listed in the available data sources in the Power Query…
    Hope this will be implemented soon…

    Reply
    • The development of Power Query for Mac is still way behind the Windows version. They’re working on it, but it’s still a way off and I don’t have an ETA for it, sorry.

      Reply
  2. Hi Mynda, I wanted to follow along in the workbook but I’m unable to download the file, the link isn’t working properly in Firefox or Edge at this time. Please advise.
    Thank you!

    Reply
  3. Creating hyper linked file name lists is a handy function, but …

    I think it would be worthwhile for you to update your article, and YouTube, to mention why access to the command has to be manually turned on.

    MS has finally disabled Excel 4.0 macros because some of the functions can be used by hackers to create malware

    Reply

Leave a Comment

Current ye@r *