Power Query: Get a list of file names from folders and sub-folders
In our previous List All File Names From A Folder And Sub-Folders tutorial, you can list all file names from a folder and the sub-folders by some basic methods, such as formula, VBA code, etc. In this article, I will introduce an amazing feature – Power Query, with this feature, you can list all files from a folder and sub-folders dynamically. After getting the filenames, when you delete or insert new files, the file name list will be refreshed automatically.
Get a list of file names from folders and sub-folders with Power Query
To achieve this task by using the Power Query feature, please do with the following steps:
1. In Excel 2016 and Excel 2021, please click Data > New Query > From File > From Folder, see screenshot:
Note: In Excel 2019 and Office 365, you should click Data > Get Data > From File > From Folder, see screenshot:
2. In the opened Browse window, choose the folder contains the files that you want to get the files names from, see screenshot:
3. Then, click Open button, a preview list of documents opens as below screenshot shown:
4. And then, click Transform Data to open the Power Query Editor window, the file attributes are displayed in the window.
5. If you want to get other attributes, such as file size, please click the expand icon in the Attributes column, it will show you a list of additional columns, select the columns that you want to have in the data, and uncheck the rest. Here, I will check Size option, see screenshot:
6. You can also delete the columns that you don’t need. In the Power Query Editor window, select the column you want to delete, right-click the column, and click Remove. See screenshot:
7. After getting the attribute columns you need, please click Close & Load > Close & Load, see screenshot:
8. Now, all the data about the files are loaded into a new worksheet as below screenshot shown:
Tips:
1. If you need the file names changed dynamically when you delete existing files or insert new files into the folder, you just need to click Data > Refresh All, and the file name list will be refreshed to the newest list.
2. With above steps, all files in folder and its sub-folders are listed. If you want to list the file names from the folder only (excluding the sub-folders), you just need to change the Folder.Files to Folder.Contents into the formula bar of the Power Query Editor window, see screenshots:
And now, only the file names and sub-folder names in the specific folder are listed, see screenshot:
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
