Showing posts with label Get Sub Directories using VBA FileSystemObject Function. Show all posts
Showing posts with label Get Sub Directories using VBA FileSystemObject Function. Show all posts

Saturday, May 22, 2010

How to iterate through all Subdirectories till the last directory in VBA

List all Level SubDirectories using VBA

The following code lists all the directories under c:\Temp

Function GetSubDir(ByVal sDir)

    Dim oFS As New FileSystemObject
    Dim oDir
    
    Set oDir = oFS.GetFolder(sDir)
    For Each oSub In oDir.SubFolders
        MsgBox oSub.Path
        GetSubDir oSub.Path
    Next oSub
End Function


You can call the function like shown below

GetSubDir "C:\Temp\"


The code uses FileSystemObject from Microsoft Scripting RunTime. You need to add reference to this library (see figure below)



See also VBA Dir Function to Get Sub Directories
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.