Excel function to list files in a folder without VBA - Excel Off The Grid (original) (raw)

Listing the files in a folder is one of the activities which cannot be achieved using normal Excel formulas. I could tell you to turn to VBA macros or PowerQuery, but then any non-VBA and non-PowerQuery users would close this post instantly. But wait! Back away from the close button, there is another option.

For listing files in a folder we can also use a little-known feature from Excel version 4, which still works today, the FILES function.

If you search through the list of Excel functions, FILES is not listed. The FILES function is based on an old Excel feature, which has to be applied in a special way. The instructions below will show you step-by-step how to use it.

Create a named range for the FILES function

The first step is to create a named range, which contains the FILES function. Within the Excel Ribbon click Formulas -> Define Name

FILES - Define Name Ribbon

Within the New Name window set the following criteria:

FILES - New Name listFiles

Click OK to close the New Name window.

Apply the function to list files

The second step is to set-up the worksheet to use the named range.

In Cell A1 (or whichever cell reference used in the Refers to box) enter the folder path from which to list the files, followed by an Asterisk ( * ). The Asterisk is the wildcard character to find any text, so it will list all the files in the folder.

Select the cell in which to start the list of files (Cell A3 in the screenshot below), enter the following formula.

=INDEX(listFiles,1)

FILES formula - first result

The result of the function will be the name of the first file in the folder.

To retrieve the second file from the folder enter the following formula

=INDEX(listFiles,2)

It would be painful to change the file reference number within each formula individually, especially if there are hundreds of files. The good news is, we can use another formula to calculate the reference number automatically.

=INDEX(listFiles,ROW()-ROW(A$2))

The ROW() function is used to retrieve the row number of a cell reference. When used without a cell reference, it returns the row number of the cell in which the function is used. When used with a cell reference it returns the row number of that cell. Using the ROWS function, it is possible to create a sequential list of numbers starting at 1, and increasing by 1 for each cell the formula is copied into.

If the formula is copied down further than the number of files in the folder, it will return a #REF! error.

FILES with errors

Finally, wrap the formula within an IFERROR function to return a blank cell, rather than an error.

=IFERROR(INDEX(listFiles,ROW()-ROW(A$2)),"")

FILES function IFERROR

Listing specific types of files

The FILES function does not just list Excel files; it lists all file types; pdf, csv, mp3, zip, any file type you can think of. By extending the use of wildcards within the file path it is possible to restrict the list to specific file types, or to specific file names.

The screenshot below shows how to return only files with “_pdf_” as the last three characters of the file name.

FILES with wildcards

The wildcards which can be applied are:

The screenshot below shows how to return only files with the name of “_New York._“, followed by exactly three characters.

FILES with wildcards 2

Advanced uses for the FILES named range

Below are some ideas of how else you could use the FILES function.

Count the number of files

The named range created works like any other named range. However, rather than containing cells, it contains values. Therefore, if you want to calculate the number of files within the folder, or which meet the wildcard pattern use the following formula:

=COUNTA(listFiles)

Wouldn’t it be great to click on the file name to open it automatically? Well . . . just add in the HYPERLINK function and you can.

FILES with hyperlinks

The formula in Cell A3 is:

=IFERROR(HYPERLINK(LEFT($A$1,LEN($A$1)-1)&INDEX(listFiles,ROW()-ROW(A$2)), INDEX(listFiles,ROW()-ROW(A$2))),"")

Check if a specific file exists within a folder

It isn’t necessary to list all the files to find out if a file exists within the folder. The MATCH function will return the position of the file within the folder.

FILES with MATCH function

The formula in cell B3 is:

=MATCH(A3,listFiles,0)

In our example, a file which contains the text “_New Yor*” exists, as the 7th file, therefore a 7 is returned. Cell B4 displays the #N/A error because “_Seattle” does not exist in the folder.

Find the name of the next or previous file

The files returned are in alphabetical order, therefore it is possible to find the next or previous file using the INDEX / MATCH combination.

FILES find the next or previous file

The next file after “_Denver.xlsx_” is “_New York.pdf_“. The formula in Cell B3 is:

=INDEX(listFiles,MATCH(A3,listFiles,0)+1)

Retrieve values from each file with INDIRECT

The INDIRECT function can construct a cell reference using text strings. Having retrieved the list of files in a folder, it would be possible to obtain values from those files.

FILES with INDIRECT

The formula in Cell B3 is:

=INDIRECT("'"&LEFT($A$1,LEN($A$1)-1)&"["&A3&"]Sheet1'!$A$1")

For INDIRECT to calculate correctly the file does need to be open, so this may be a significant flaw in this option.

Usage notes

When working with the FILES function there are a few things to be aware of:

Further reading

There are variety of other Excel 4 functions available which still work in Excel. Check out this post to find out how to apply them and download the Excel 4 Macro functions reference guide.

If you decide to use a VBA method, check out this post.


Discover how you can automate your work with our Excel courses and tools.

The Excel Academy

Excel Academy

Make working late a thing of the past.

The Excel Academy is Excel training for professionals who want to save time.