Excel Directory Listing Help

Armster

Board Regular
Joined
Jul 26, 2007
Messages
63
I have the following script that I need a little help with adjusting

Basically the script goes to specified directory and lists the subfolders in the directory, is there any way I can stop it doing EVERY subdirectory of the subdirectories and only just list the direct subfolders within the specified folder?

Thanks for your help in advance

Dave

Code:
Sub TestListFolders()
    Application.ScreenUpdating = True
    ' create a new workbook for the folder list
    ' add headers
    With Range("A1")
        .Formula = "Folder contents:"
        .Font.Bold = True
        .Font.Size = 12
    End With
    Range("A3").Formula = "Folder Path:"
    Range("B3").Formula = "Folder Name:"
    Range("A3:G3").Font.Bold = True
    ListFolders "\\yorkshire2\Global\T - Technical\Sites\", True
    Application.ScreenUpdating = True
End Sub

Sub ListFolders(SourceFolderName As String, IncludeSubfolders As Boolean)
' lists information about the folders in SourceFolder
' example: ListFolders "C:\FolderName", True
Dim FSO As FileSystemObject
Dim SourceFolder As Folder, SubFolder As Folder
Dim r As Long
    Set FSO = New FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    ' display folder properties
    r = Range("A65536").End(xlUp).Row + 1
    Cells(r, 1).Formula = SourceFolder.Path
    Cells(r, 2).Formula = SourceFolder.Name
'    Cells(r, 3).Formula = SourceFolder.Size
'    Cells(r, 4).Formula = SourceFolder.SubFolders.Count
'    Cells(r, 5).Formula = SourceFolder.Files.Count
'    Cells(r, 6).Formula = SourceFolder.ShortName
'    Cells(r, 7).Formula = SourceFolder.ShortPath
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFolders SubFolder.Path, True
        Next SubFolder
        Set SubFolder = Nothing
    End If
    Columns("A:G").AutoFit
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
Option Explicit

Sub TestListFolders()
    Application.ScreenUpdating = True
    ' create a new workbook for the folder list
    ' add headers
    With Range("A1")
        .Formula = "Folder contents:"
        .Font.Bold = True
        .Font.Size = 12
    End With
    Range("A3").Formula = "Folder Path:"
    Range("B3").Formula = "Folder Name:"
    Range("A3:G3").Font.Bold = True
    ListFolders "\\yorkshire2\Global\T - Technical\Sites\", True, True
    Application.ScreenUpdating = True
End Sub

Sub ListFolders(SourceFolderName As String, _
                IncludeSubfolders As Boolean, _
                InitialCall As Boolean)
' lists information about the folders in SourceFolder
' example: ListFolders "C:\FolderName", True
Dim FSO As FileSystemObject
Dim SourceFolder As Folder, SubFolder As Folder
Dim r As Long
    Set FSO = New FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    ' display folder properties
    r = Range("A65536").End(xlUp).Row + 1
    Cells(r, 1).Formula = SourceFolder.Path
    Cells(r, 2).Formula = SourceFolder.Name
'    Cells(r, 3).Formula = SourceFolder.Size
'    Cells(r, 4).Formula = SourceFolder.SubFolders.Count
'    Cells(r, 5).Formula = SourceFolder.Files.Count
'    Cells(r, 6).Formula = SourceFolder.ShortName
'    Cells(r, 7).Formula = SourceFolder.ShortPath
    If InitialCall Then
        If IncludeSubfolders Then
            For Each SubFolder In SourceFolder.SubFolders
                ListFolders SubFolder.Path, True, False
            Next SubFolder
            Set SubFolder = Nothing
        End If
    End If
    Columns("A:G").AutoFit
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,239
Members
448,555
Latest member
RobertJones1986

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