In the last post, we saw how to import all the files from a folder, which is a fantastic time-saving technique. While focusing on this aspect, we skipped over another useful part of Power Query; which is being able to obtain a list of all the files in a folder, and sub-folders, along with the file attributes.
I have previously written posts about how to list all the files in a folder using VBA and also using a legacy Excel 4 function. These posts still receive lots of search traffic from Google, so I know this is an area which users are interested in. However, in Power Query, we now have the easiest method of all.
List all the files in a folder (and sub-folders)
In Excel, click Data -> Get Data-> From File -> From Folder.
In the Folder window click Browse…, navigate to the folder then click OK.
When the list of documents opens click Transform Data.
The Power Query Editor opens and shows some useful attributes:
- Data accessed
- Date modified
- Date created
- Folder Path
Move the Name column to the end, then select the Name and Folder Path columns, click Add Column -> Merge Columns.
On the Merge Columns window, choose no separator and click OK.
A new column is created with the complete file path. Make the following transformations:
- Rename the new column.
- Remove all except the new column.
The Preview window shows a list of all the files and folders.
Click Home -> Close and Load to push the data into Excel.
How easy was that!
In the section above, we saw some file attributes, mostly based on dates, but this is just the start. The Attributes column in the Preview Window provides access to lots of other information.
Click on the expand icon in the header of the Attributes column, then click OK to see the file attributes. I’m not going to lie… I have no idea what some of these attributes are, but others very useful, such as:
- Kind – the file type
- Size – the file size
- Read-only – whether the file is currently set to Read-Only
- Hidden – whether the file is visible
Imagine that your disk space is getting low. Using this method, you can quickly identify the largest file sizes, or the files which have not been opened recently, or duplicate file names. Once you have this list, you can decide what to keep and what to delete.
Attributes don’t finish at the file level either. Follow the steps above to navigate a folder containing Excel files. Filter to include only the Excel files you’re interested in. Your Preview Window might look like this.
Click Add Column -> Custom Column.
In the Custom Column window to enter the following settings:
Column Name: ExcelFile
Custom column formula:
Click the Expand icon on the ExcelFile column to drill into each file.
The attributes here provide:
- Worksheet name
- Kind – worksheet or table
- Hidden – whether the worksheet is hidden
There you have it. A useful little trick to get information about files. Even today, I used this technique to check whether an automated process exported the same files as last month. Now I can click refresh and this check is completed in seconds.
Power Query Series Contents
- Import Data
- Data Refresh
- Edit Queries
- Close & Load Options
- Using Parameters
- Basic Transformations
- Combine / Append Queries
- Import All Files in a Folder
- List All the Files in a Folder and File Attributes
- Import Data from the Current Workbook
- Import Data from the Web
- Unpivot Data
- Unstacking Data in a Column
- Lookup Values using Merge
- Change the Source Data Location
- If Statements for Conditional Logic
- Grouping and Summarizing Data
- Custom Functions
- Common Errors & How to Fix Them
- Tips and Tricks