List folders contained in a folder

triman

New Member
Joined
Jul 28, 2011
Messages
2
I want to list all folder names BUT NOT OTHER FILES within a specified folder. For simplicity, output to a sheet.

I would like to know functions down to their roots so I can learn more so please try to use the least amount of already known functions. If not, oh well, as long as I have the ability to do what I need I'll be happy.

Thanks!
Kevin
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Straight from the excel help file ...

Code:
Sub ShowFolderList(folderspec)
    Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
    For Each f1 In fc
        s = s & f1.Name
        s = s & vbCrLf
    Next
    MsgBox s
End Sub
 
Upvote 0
Commented it heaps for you, post back if there is anything you don't understand

Code:
Sub FindFolder()
Dim DirName As String
Dim SearchFolder As String
Dim X As Long
SearchFolder = "C:\Temp\"
X = 1
DirName = Dir(SearchFolder, vbDirectory) 'Return files and folders
Do Until DirName = "" 'Loop through all results
    If DirName <> "." And DirName <> ".." Then 'Omit the . and ..
            If GetAttr(SearchFolder & DirName) = 16 Then 'Is it a folder? (16 = Folder, 32 = File)
                Range("A" & X).Formula = DirName 'Post the name to the row defined by X
                X = X + 1 'Add one to X for the next row
            End If
    End If
    DirName = Dir() 'Grab the next filename
Loop
End Sub
 
Upvote 0
Blade: This is great! Its exactly what I needed. I did see the other method in the help but this helps me understand in a bigger perspective. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top