Macro to List Folders excluding Data Stamp within E:\Cobian

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have code to list folders excluding the date stamp eg E:\Cobian\ProgramData 2024-10-25 02;11;55 (Full) i.e. list only E:\Cobian\ProgramData in Col F on sheet subfolders and show no.of folders for each folder name in Col G

My code is also counting the sub-folders within these folders which I do not require eg application data with Program Data

It would be appreciated if someone could kindly amend my code

Code:
 Sub ListUniqueFoldersAndCounts()
    Dim folderPath As String
    Dim fso As Object
    Dim folderDict As Object
    Dim subFolder As Object
    Dim baseName As String
    Dim ws As Worksheet
    Dim rowNum As Long
    Dim key As Variant
    Dim folder As Object
    Dim folderName As String
    
    ' Define the folder path (root folder)
    folderPath = "E:\Cobian"
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Subfolders")
    
    ' Initialize FileSystemObject and Dictionary
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folderDict = CreateObject("Scripting.Dictionary")
    
    ' Loop through each folder in the root directory (skip subfolders and sub-subfolders)
    For Each folder In fso.GetFolder(folderPath).SubFolders
        ' Get the folder name without date/time or extra text (split on space, take the first part)
        folderName = folder.Name
        baseName = Trim(Split(folderName, " ")(0)) ' This will take only the first part of the folder name before the timestamp
        
        ' Ensure the dictionary for this base name exists
        If Not folderDict.exists(baseName) Then
            folderDict.Add baseName, 0 ' Initialize with 0 count
        End If
        
        ' Increment the folder count for each base name (only count first-level folders)
        folderDict(baseName) = folderDict(baseName) + 1
    Next folder
    
    ' Clear previous data in columns F and G
    ws.Range("F:G").ClearContents
    
    ' List unique folder names in Column F and their counts in Column G
    rowNum = 2 ' Start from row 2
    For Each key In folderDict.Keys
        ws.Cells(rowNum, 6).Value = key ' List base name in Column F
        ws.Cells(rowNum, 7).Value = folderDict(key) ' Folder count in Column G
        rowNum = rowNum + 1
    Next key
    
    MsgBox "Unique folders and their counts have been listed.", vbInformation
End Sub [code]
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,225,347
Messages
6,184,429
Members
453,231
Latest member
HerGP

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