Friday, February 5, 2016

Excel VBA- FSO List sub folders

EXCEL VBA LIST ALL SUBFOLDERS OF A FOLDER & THEIR PROPERTIES


Sometimes we have the requirement to find the properties of several sub folders of a folder. For example, we may need the information about the size, name, full path etc. of sub folders. To access these information manually is quite tedious. The following VBA code can help us to find such information.

Sub pListSubFoldersProperties()
    
    Dim FSO As Object
    Dim objFldr As Object
    Dim strFldrPath As String
    Dim SubFldr As Object
    Dim varRetrn
    Dim lngC As Long
    
    Application.ScreenUpdating = False
    On Error GoTo lblEXIT
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        varRetrn = .Show
        If varRetrn <> -1 Then
            MsgBox "Cancelled."
            GoTo lblEXIT
        Else
            strFldrPath = .SelectedItems(1)
        End If
    End With
    Set objFldr = FSO.GetFolder(strFldrPath)
    
    Sheet1.Range("A:E").Clear
    For Each SubFldr In objFldr.SubFolders
        lngC = lngC + 1
        Sheet1.Range("A" & lngC) = SubFldr.Name
        Sheet1.Range("B" & lngC) = SubFldr.Path
        Sheet1.Range("C" & lngC) = SubFldr.Size
    Next

lblEXIT:

    If Err.Number <> 0 Then
        MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description
    End If
End Sub

No comments:

Post a Comment

Hot Topics