This question already has an answer here:
I'm looking for a VBA script that will loop through all subfolders of a specified folder. When I say all subfolders, I mean each folder inside the specified folder, and each folder inside of that, and each folder inside of that...in theory there could be infinite nested subfolders, but in actuality it will probably not go above 3 or 4. I'm using the VBA Scripting Runtime objects, so that once I loop into the folder I can check properties of some files (but I know how to do that part).
Thank you for your help!
This question is different from the listed "similar" questions in the previous questions contained known directories, whereas the need here was to find known and unknown directories. Also needed multiple layers of subdirectories. You guys really should just read the question before you fire off "duplicate".
Just a simple folder drill down.
And to complement Rich's recursive answer, a non-recursive method.
You can use a queue for FIFO behaviour (shown above), or you can use a stack for LIFO behaviour which would process in the same order as a recursive approach (replace
Set oFolder = queue(1)
withSet oFolder = queue(queue.Count)
and replacequeue.Remove(1)
withqueue.Remove(queue.Count)
, and probably rename the variable...)