- I want to get a list of all sub-directories within a directory.
- If that works I want to expand it to a recursive function.
However my initial approach to get the subdirs fails. It simply shows everything including files:
sDir = Dir(sPath, vbDirectory)
Do Until LenB(sDir) = 0
Debug.Print sDir
sDir = Dir
Loop
The list starts with '..' and several folders and ends with '.txt' files.
EDIT:
I should add that this must run in Word, not Excel (many functions are not available in Word) and it is Office 2010.
EDIT 2:
One can determine the type of the result using
iAtt = GetAttr(sPath & sDir)
If CBool(iAtt And vbDirectory) Then
...
End If
But that gave me new problems, so that I am now using a code based on Scripting.FileSystemObject
.
Here is a VBA solution, without using external objects.
Because of the limitations of the
Dir()
function you need to get the whole content of each folder at once, not while crawling with a recursive algorithm.EDIT
This version digs into subfolders and returns full path names instead of returning just the file or folder name.
Do NOT run the test with on the whole C drive!!
You would be better off with the FileSystemObject. I reckon.
To call this you just need, say: listfolders "c:\data"
Updated July 2014: Added
PowerShell
option and cut back the second code to list folders onlyThe methods below that run a full recursive process in place of
FileSearch
which was deprecated in Office 2007. (The later two codes use Excel for output only - this output can be removed for running in Word)PowerShell
FSO
withDir
for filtering file type. Sourced from this EE answer which sits behind the EE paywall. This is longer than what you asked for (a list of folders) but i think it is useful as it gives you an array of results to work further withDir
. This example comes from my answer I supplied on another site1. Using
PowerShell
to dump all folders below C:\temp into a csv file2. Using
FileScriptingObject
to dump all folders below C:\temp into Excel3 Using
Dir
Here is a Simple version without using
Scripting.FileSystemObject
because I found it slow and unreliable. In particular the.Name
method, was slowing everything down. Also I tested this in Excel but I don't think anything I used wouldn't be available in Word.First some functions:
This joins two strings to create a file path, similar to
os.path.join
in python. It is useful for not needing to remember if you tacked on that "\" at the end of your path.This create a collection of sub items of root directory
root_path
This creates a collection of sub items in directory
root_path
that including folders and then removes items that are not folders from the collection. And it can optionally remove those nasty.
and..
foldersFinally is the recursive search function based on someone else function from this site that used
Scripting.FileSystemObject
I haven't done any comparison tests between it and the original. If I find that post again I will link it. Notecollec
is passed by reference so create a new collection and call this sub to populate it. PassvbType:=vbDirectory
for all sub folders.