Power Query: Fold(er) Away
27 February 2019
Welcome to our Power Query blog. This week, I look at some of the M functionality for dealing with folders.
Folder.Contents()
This returns a table containing the properties and contents of the files and folders found at path:
Folder.Contents(path as text) as table
where path is the path to the folder to retrieve contents for.
I will use Folder.Contents() to show the contents of my Power Query Blog folder:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/117/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
The M code I have used is
=Folder.Contents("C:\Users\kathr\OneDrive\Documents\PQ_StandardExpenses\PQ Blog")
When I execute the code, I get the following screen:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/117/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
This is a table of the names and some of the attributes of the files in the folder I specified. More detailed attributes are also available under the Attributes column, which may be expanded if required.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/117/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
Folder.Files()
This returns a table containing a row for each file found at a folder path, and subfolders. Each row contains properties of the folder or file and a link to its content.
Folder.Files(path as text) as table
where path is the path to the folder to retrieve contents for.
I will use Folder.Files() to show the contents of the folder above my Power Query Blog folder:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/117/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
The M code I have used is
=Folder.Files("C:\Users\kathr\OneDrive\Documents\PQ_StandardExpenses")
When I execute the code, I get the following results:
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/117/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I have reordered the columns to show that Folder.Files() also retrieves information about files in subfolders below the specified folder. This can be compared to the results I get if I use Folder.Contents() on the same folder.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/117/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
Folder.Contents() only shows me those files and folders that are in the specified folder, and does not interrogate data in the subfolders. Next time I will look at an example which extracts file data according to more specific conditions.
Come back next time for more ways to use Power Query!