Subdirectories

S

Skip

Hello all,

I've seen various Macros that will list files in a Directory. I'm looking
for a Macro that will list subdirectories in a directory. I would like the
Macro result printed to a blank sheet in a single column just containing the
sub-directory name. It only needs to go down 1 level - don't need
sub-directories in sub-directories. The plan is to use this data to populate
a listbox - unless the listbox could be populated directly.

Is this possible?

Many thanks.

Paul.
 
J

Jake Marx

Hi Paul,
I've seen various Macros that will list files in a Directory. I'm
looking for a Macro that will list subdirectories in a directory. I
would like the Macro result printed to a blank sheet in a single
column just containing the sub-directory name. It only needs to go
down 1 level - don't need sub-directories in sub-directories. The
plan is to use this data to populate a listbox - unless the listbox
could be populated directly.

You can use the FileSystemObject to do this pretty easily. The following
code assumes you have a ListBox on Sheet1 named "lstSubFolders":

Sub PopulateSubFolders()
Dim fso As Object
Dim fol As Object

Sheet1.lstSubFolders.Clear

Set fso = CreateObject("Scripting.FileSystemObject")
For Each fol In fso.GetFolder("C:\Program Files").SubFolders
Sheet1.lstSubFolders.AddItem fol.Name
Next fol

Set fso = Nothing
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top