List folders without recursive

englicon

New Member
Joined
Jan 21, 2013
Messages
3
Hi all,
I'm trying to figure out a way to list only the first level of folders in a file structure. I'm accustomed to using the GetFolder method but how to do only one level is the question. For instance, if I had the following folder structure:

C:\Base
C:\Base\FolderA
C:\Base\FolderA\Folder1
C:\Base\FolderA\Folder2
C:\Base\FolderB
C:\Base\FolderB\Folder3

and I set the macro to scan C:\Base then it would return:
C:\Base\FolderA
C:\Base\FolderB

Does that make sense? Please note that I know how to list the files. But I want to list the folders.

Right now I have my macro as something like:
Set FSO = CreateObject("scripting.filesystemobject")
Set theBaseFolder = FSO.GetFolder(theBaseFolderName)
For Each ofTheFolders in theBaseFolder.SubFolders

But that's where I get stuck. Right now this returns ALL of the subfolders. I could pull the path of each object as a string and search for the number of "\" contained but that is a clunky solution for something that seems like it should be fairly clean.

Suggestions? Thoughts?
I'm not really that great of a programmer so a little explanation would be helpful. Otherwise I will play with the code suggestions until they make sense to me.

Thanks!
 
Code:
Sub LS()

    Dim FSO As Object
    Dim theBaseFolder As Object, subfolder As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set theBaseFolder = FSO.GetFolder("C:\Base")
    For Each subfolder In theBaseFolder.SubFolders
        Debug.Print subfolder.Path
    Next
    
End Sub
 
Upvote 0
Thanks John_w. I get it. The difference is that I was putting in "Next subfolder" when I should just be putting in "Next".


Code:
Sub LS()

    Dim FSO As Object
    Dim theBaseFolder As Object, subfolder As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set theBaseFolder = FSO.GetFolder("C:\Base")
    For Each subfolder In theBaseFolder.SubFolders
        Debug.Print subfolder.Path
    Next
    
End Sub
 
Upvote 0

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