In a previous post I looked at using the FileSystemObject to search through folders on your computer's disk and create a hyperlinked list of files. Clicking on the hyperlink would open the file.
FileSystemObject (FSO) can also be used to retrieve meta data from a file, like the creation date, last modified date or file size.
In this post we'll look at modifying the code from my fist post on this topic, and creating a hyperlinked list of files with some file meta data.
Please read my first post as it covers some important steps like creating a reference to the Microsoft Scripting Library, without which this code won't work. It also has an explanation of recursion which is very useful programming concept to understand, and which this code uses.
Plus, I'm going to create a userform to ask what meta data we want listed
File Meta Data
Using FileSystemObject.GetFile we can access various file meta data, some of which I am not interested in for this example. I'm going to use DateCreated, DateLastModified, Size and Type.
To see the full list of meta data you can access, in the VBA editor, use the auto-complete list to see what is available:
User Form
The userform is pretty simple, just 4 check boxes and a text box surrounded by a frame, and two buttons.
Each checkbox corresponds to some meta data that I want to retrieve. The text box allows the user to type in their search term e.g. *.xl* if they are looking for Excel workbooks. The function of the buttons is self explanatory.
You could enhance the form by adding checkboxes for common file types e.g. Excel Workbooks, Excel Macro Enabled Workbooks, or PDF's. This will reduce what has to be typed, and perhaps even remove the need for a text box altogether.
How Does the Form Work?
When a check box is checked, its value is True. If it is unchecked its value is False.
So to see what meta data we need to look for we just test to see what checkbox values are true, e.g.
If FileSize Then
Because the checkbox is a Boolean object, it can only be true or false, I don't have to explicitly test for the values True or False like so
If FileSize.Value = True Then
The textbox is used to specify the filename pattern to match e.g. *.xls? which matches all files with extensions .xlsm or .xlsx. The wildcards * (0 or more characters) and ? (a single character) can be used.
Picking the Folder to Search
Once we know what meta data and files to look for we need to pick a folder to search. To do this we use the Application.FileDialog property
This allows us to choose a folder and then use the Dir function to search that folder, and its subfolders. This is done recursively, check my first article on this to see an explanation of a recursive routine.
The List of Files
As the code searches our selected folder (and its subfolders), we get a list on our worksheet of any files found. The name of the file is hyperlinked to the file, so clicking the link will open that file.
Right beside the filename/hyperlink is the meta data we said we wanted.
Acknowledgement
Thanks to our Excel guru Catalin for some of the FSO code that went into this routine.
Example Workbook
In the example workbook, click on the blue button at the top of Sheet1 to start the macro.
Enter your email address below to download the sample workbook.
You can download the workbook from here
Hello, I’m using your Code of List of Files with great interest; but I have a line failure: Current Filename = Dir(TargetFiles, 7) can I be helped to solve this problem. Thank you for your support, it is really appreciated.
Hi André
Difficult to say what’s causing that without seeing your workbook and knowing what inputs you are providing to the code.
TargetFiles is constructed from the line TargetFiles = StartingFolder.Path & “\” & FileType so what are the values for StartingFolder.Path and FileType?
It could be the path separator is causing you issues. Try changing
TargetFiles = StartingFolder.Path & “\” & FileType
to this
TargetFiles = StartingFolder.Path & Application.PathSeparator & FileType
If that doesn’t work you can try debugging the code.
Or post a question on our forum and tell me what folder you are searching in and what file type you are looking for when this error occurs.
Regards
Phil
Thanks for giving such a nice article . The information you shared will help me to get a good knowledge.
You’re welcome Esther.
Very cool Phil! Thanks for sharing. I like the format of the output. It would be great to also have an output in a tabular format in an Excel Table.
Thanks John. Good suggestion. Shouldn’t take to much work to recode with a table as output.
Cheers
Phil