How can I exclude subfolders in a subfolder

Ascherith

New Member
Joined
Feb 14, 2024
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello!

I am new here and I hope you can help me as I am a noob when in comes to macros.

I have a macro that I want to create that can list down all the file name and the file path of each item inside a folder.
I also want this macro to list the items inside a subfolder. Note that there is also a subfolder inside the subfolder but I don't want to list down the files on the deeper subfolders.
Please see the screenshots below.
When I choose the folder name " Folder to Check", what I need is for the macro to run through all the sub folders "1 to 5" listing down all the PDF file names but will not include the PDF files inside the subfolder ABC.
Please note that these are just sample folder names but it is basically the logic of it. Some subfolders have multiple subfolders inside which I want to exclude with out specifying their names. Is this possible? Can you please edit the below code if it can be done on that? Thank you very much and I would really appreciate it!

1707910372890.png


1707910420878.png


1707910448191.png


Excel Formula:
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder
Next oSubfolder
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
When I choose the folder name " Folder to Check", what I need is for the macro to run through all the sub folders "1 to 5" listing down all the PDF file names but will not include the PDF files inside the subfolder ABC.
Please note that these are just sample folder names but it is basically the logic of it. Some subfolders have multiple subfolders inside which I want to exclude with out specifying their names.

There's no need for the code to exclude the subfolders of the first level subfolders by their names. You want to list the PDF files in just the first level subfolders of the chosen "Folders to check" folder, so the code below simply loops through the subfolders and has a nested loop to loop through the files in each subfolder.

VBA Code:
Public Sub List_PDF_Files_In_First_Level_Subfolders()

    Dim startFolderPath As String
    Dim startCell As Range, n As Long
    Dim FDfolder As FileDialog
    Dim FSO As Object 'Scripting.FileSystemObject
    Dim FSsubfolder As Object 'Scripting.Folder
    Dim FSfile As Object 'Scripting.File
    
    Set FDfolder = Application.FileDialog(msoFileDialogFolderPicker)
    
    With FDfolder
        .Title = "Select Folder"
        If Not .Show Then
            MsgBox "User cancelled", vbInformation
            Exit Sub
        End If
        startFolderPath = .SelectedItems(1)
    End With
    
    With ThisWorkbook.Worksheets(1)
        .Activate
        .Cells.Clear
        .Range("A1").Value = "Path"
        Set startCell = .Range("A2")
    End With
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    n = 0
    For Each FSsubfolder In FSO.GetFolder(startFolderPath).SubFolders
        For Each FSfile In FSsubfolder.Files
            If FSfile.Name Like "*.pdf" Then
                startCell.Offset(n).Value = FSfile.Path
                n = n + 1
            End If
        Next
    Next
        
End Sub
 
Upvote 1
There's no need for the code to exclude the subfolders of the first level subfolders by their names. You want to list the PDF files in just the first level subfolders of the chosen "Folders to check" folder, so the code below simply loops through the subfolders and has a nested loop to loop through the files in each subfolder.

VBA Code:
Public Sub List_PDF_Files_In_First_Level_Subfolders()

    Dim startFolderPath As String
    Dim startCell As Range, n As Long
    Dim FDfolder As FileDialog
    Dim FSO As Object 'Scripting.FileSystemObject
    Dim FSsubfolder As Object 'Scripting.Folder
    Dim FSfile As Object 'Scripting.File
   
    Set FDfolder = Application.FileDialog(msoFileDialogFolderPicker)
   
    With FDfolder
        .Title = "Select Folder"
        If Not .Show Then
            MsgBox "User cancelled", vbInformation
            Exit Sub
        End If
        startFolderPath = .SelectedItems(1)
    End With
   
    With ThisWorkbook.Worksheets(1)
        .Activate
        .Cells.Clear
        .Range("A1").Value = "Path"
        Set startCell = .Range("A2")
    End With
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    n = 0
    For Each FSsubfolder In FSO.GetFolder(startFolderPath).SubFolders
        For Each FSfile In FSsubfolder.Files
            If FSfile.Name Like "*.pdf" Then
                startCell.Offset(n).Value = FSfile.Path
                n = n + 1
            End If
        Next
    Next
       
End Sub
Hi! Thank you very much! I added a separate column for the file name of the PDF and some alignments. Works perfectly!
 
Upvote 0
Hello, I have another question regarding the file type. What is the syntax if I want the macro to read only pdf files both upper and lowercase? Because my current macro is only reading the lowercase pdf. Thank you.

Excel Formula:
For Each FSsubfolder In FSO.GetFolder(startFolderPath).SubFolders
        For Each FSfile In FSsubfolder.Files
            If FSfile.Name Like "*.pdf" Then
                startCell.Offset(n).Value = FSfile.Path
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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