Skip to: Site menu | Main content

Login

Name: 
Password:
Remember me?
Register

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() 
     
     'declares
    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("wscript.shell") 
     '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 
    Next 
    MsgBox strFolders 
     
End Sub