Late answer, but posting for others who might have a similar problem.
I had a similar challenge but had the restriction of not being able to use FileSystemObject
. Therefore, I wrote a Class library that makes heavy use of the Dir() function to parse all the files and folders in a specified directory. It requires you to set no references to additional libraries in the VBA IDE.
You can use it like this:
Sub PrintFilesAndFolders(Directory As DirectoryManager, Optional indent As String)
'Helper method
Dim folder As DirectoryManager
Dim file As DirectoryManager
Dim newIndent As String
For Each folder In Directory.Folders
Debug.Print indent & "+ " & folder.Name
newIndent = indent & " "
PrintFilesAndFolders folder, newIndent
Next folder
For Each file In Directory.Files
Debug.Print indent & "- " & file.Name
Next file
End Sub
Sub LoopThroughAllFilesAndFolders()
Dim dm As DirectoryManager
Set dm = New DirectoryManager
dm.Path = ThisWorkbook.Path & "\Sample Data Set"
PrintFilesAndFolders dm
End Sub
In the helper function, you can substitute the file.Name
with file.Path
, and instead of Debug.Print
just write the output to your target workbook cell.
As far as watching for files changing on the system, I don't know of a way for Excel to automatically do that. At best, I think you would be able to write a script that runs when the workbook starts up to go reparse all the folders and files again and repopulate the workbook.