List folders in directory, with criteria

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
Apologies if this is a considered a duplicate post.

This macro lists first and second level sub-folders after the selected folder "folderspec", with the condition that any of the subfolders' name contains the string "OK" (sub-folder with this string can be at any level).
The output is divided into separate columns starting at column J.

The macro has the following inconsistency:
- It only works if the subfolder which contains the string "OK" is the first one in the set.
LISTED: {Subfolder 3-A OK, Subfolder 3-B}
NOT LISTED: {ASubfolder, Subfolder 3-A OK, Subfolder 3-B}

Notes on code:
- Microsoft Scripting Runtime required as reference (Tools/References)
- Specify directory in variable "folderspec"
- Specify word/string in Function (in this case "OK")
- Output starts at cell J1

Code:
'References: Microsoft Scripting Runtime

Dim Exists As Boolean

Sub Regather()
    Dim fs, fc, fcsub
    Dim f As Scripting.Folder, f1 As Scripting.Folder
    Dim n As String
    Dim r As Integer, C As Integer
    Dim folderspec As String
    Dim OriginalLocation As Range
    Application.ScreenUpdating = False
    Set OriginalLocation = Selection
    Range(Range("J1"), Cells(Rows.Count, Columns.Count)).ClearContents
    folderspec = "C:\" '********** Directory
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.subfolders
    n = f.Name
    r = 1
    C = 1
    ' Loop through folders
    For Each f1 In fc
        Exists = False
        Call LookForFolder(f1, True)
        If Exists = True Then
            Cells(r + 1, C + 9) = f1.Name
            Set fcsub = f1.subfolders
            r = r + 1
                For Each f2 In fcsub
                    Cells(r + 2, C + 9) = f2.Name
                    r = r + 1
                Next
            C = C + 1
            r = 1
         End If
    Next
End Sub

Function LookForFolder(objFolder As Scripting.Folder, IncludeSubfolders As Boolean)
    Dim objSubFolder As Scripting.Folder
    If InStr(objFolder.Path, "OK") <> 0 Then '********** Word looking for
        Exists = True
    End If
    If IncludeSubfolders Then
        For Each objSubFolder In objFolder.subfolders
            Call LookForFolder(objSubFolder, True)
            Exit Function
        Next objSubFolder
    End If
End Function
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You will need to extract ALL filenames in your directory structure to the desired level to an array (or worksheet) then examine the array for the matching word.
As you go to the next layer of subfolders, increment a depth counter to limit how far down you look.
Code in this post: https://www.mrexcel.com/forum/excel-questions/480404-vba-list-folder-names-size.html
Produces a list of all folders below a selected starting folder
 
Upvote 0
Hi. I don't quite have the knowledge to modify the code you referred to.
Is there a way to include a counter into the code in OP?
 
Upvote 0
Hi

just another idea:

Use CMD.Exe with

Code:
dir /s/ad > myList.txt

to get all names of subdirectories, import the txt-file into xl and filter.

regards
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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