List the subdiretory of a specific directory

A

Alex St-Pierre

Hello,
There is a function which allow me to have the list
of files in a directly (see below). What I need is to have
the list of all "sub directories" in a specific folder. I
want the name of all subdirectory in a worksheet. Does
anyone knows what is the modification I should do?

Thank you

Alex

Set fs = Application.FileSearch
With fs
.LookIn = "C:\Windows"
.SearchSubFolders = True
.FileName = "cmd*"
If .Execute() > 0 Then
MsgBox "This folder contains " & .FoundFiles.Count
& _
" file(s)."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "No file as been found."
End If
End With
 
M

Martin Seelhofer

Hi Alex

You might want to use the FileSystemObject of the Scripting-
Library. Here's code which works without referencing the
library (late-binding):

' returns all subfolder names in a collection
Function ListSubFolders(fldr As String) As Collection
Dim col As New Collection
Dim fso As Object
Dim objFolder As Object
Dim el
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(fldr)
For Each el In objFolder.Subfolders
col.Add el.name
Next
Set ListSubFolders = col
End Function

' demonstrates the use of the above routine
Sub ListSubFoldersTest()
Dim col As Collection
Dim el
Set col = ListSubFolders("D:\Programme")
For Each el In col
Debug.Print el
Next
End Sub


However, if you want to get deeper into the Scripting-library,
you should add a Reference to it (it's called the "Microsoft
Scripting Runtime") and use the specific types instead of just
Object...


Cheers,
Martin
 
K

Karl E. Peterson

Alex St-Pierre said:
Hello,
There is a function which allow me to have the list
of files in a directly (see below). What I need is to have
the list of all "sub directories" in a specific folder. I
want the name of all subdirectory in a worksheet. Does
anyone knows what is the modification I should do?

Here's a few hundred solutions:

http://groups.google.com/groups?as_q=recursive dir&as_ugroup=*vb*&num=50

Long live Google! :)

Later... Karl
 

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