Skip to: Site menu | Main content


Remember me?

Listing Subfolders Using the FileSystemObject

written by Mark Rowlinson - Last updated Mar 2006

This piece of code will list all folders on the desktop. To run it you will need to add a reference to the microsoft scripting library.

Sub ListFoldersOnDesktop() 
    Dim fso As Scripting.FileSystemObject 
    Dim f As Scripting.Folder 
    Dim w As Object 
    Dim strPath As String 
     'create a shell object to get desktop folder with
    Set w = CreateObject("") 
     'get desktop folder path - other constants are available
    strPath = w.specialfolders("Desktop") 
     'create fso
    Set fso = New Scripting.FileSystemObject 
     'get a reference to the desktop
    Set f = fso.GetFolder(strPath) 
    Dim s As Scripting.Folder 
    strFolders="Folders:" & vbnewline 
     'loop through the folders on the desktop
    For Each s In f.SubFolders 
        strFolders=strFolders & vbNewLine & s.Name 
    MsgBox strFolders 
End Sub