Count subfolders ending in x (1 Viewer)

AshDash

New member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2022
Messages
3
Back story: my workplace has a system where there is a folder with a number as a title 2335, 2187, 2*** etc. Each folder contains a word document, some digital images and sometimes a pdf.
This 2*** folder gets dropped into our 'Location' folder by an external stakeholder. There are almost 40 different 'Location' folders. I have a nice little access document that has buttons that change from green to red when a 'Location' folder receives a folder beginning with a #2.

The 2*** moves into an 'Indexed' folder once some information is placed into an excel spedsheet and given a priority at the end so it will look like 2*** Y, 2*** G, etc

When it has been dealt with it moves to the 'Completed' folder

Work wants a count oh how many folders are in the Location\Indexed folder and preferably a count of how many folders end with Y or G

I can only find scripts that assist with file counts in folders, not folder counts.

Is this even possible?

Edit: fat finger hit send button before I had finished typing.
 
Last edited:

AshDash

New member
Local time
Tomorrow, 01:37
Joined
Jan 20, 2022
Messages
3
Thanks, my googling would only bring up count files in folder.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:37
Joined
Sep 21, 2011
Messages
14,053
Thanks, my googling would only bring up count files in folder.
At least two of those links appear to use the same method. You just then need to count for your last character required.
FWIW the phrase I used was 'count subfolders vba'
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:37
Joined
Oct 29, 2018
Messages
21,358
You can use either FSO or Dir() to count them using the proper attributes. For example, Dir() has vbDirectory and FSO has the Folders collection.
 

jdraw

Super Moderator
Staff member
Local time
Today, 10:37
Joined
Jan 23, 2006
Messages
15,364
Thanks, my googling would only bring up count files in folder.
This function will get/list all subfolders under a given folder. It may be a starting point.

Code:
Function FolderCount(dirpath As String, Optional namematch As String) As Long
'https://www.mrexcel.com/board/threads/count-number-of-folders.386836/
Dim fso As Object, mydir As Object, tempdir As Object, i As Long

Set fso = CreateObject("Scripting.FileSystemObject")
Set mydir = fso.GetFolder(dirpath)

For Each tempdir In mydir.SubFolders
Debug.Print tempdir.name
    If IsMissing(namematch) Then
        i = i + 1
    Else
        If InStr(tempdir.name, namematch) > 0 Then i = i + 1
    End If
Next

Set mydir = Nothing
Set fso = Nothing
FolderCount = i
End Function

This Command
C:\users\..... > dir /A:D /B /S to produce a list of all folders and all subfolders of the directory.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:37
Joined
May 7, 2009
Messages
19,175
another alternative:
Code:
' https://serverfault.com/questions/303029/how-to-list-the-top-level-folders-of-a-drive-using-vbscript
'
' arnelgp
'_____________________________________________________________________
' rootfolder    the name of the folder where each subfolder is located
' wildCard      any part of the subfolder name, like "*X"
'               the subfolder name ends with "X"
'_____________________________________________________________________
Public Function getFolderCount(ByVal rootFolder As String, Optional ByVal wildCard As String = "*") As Integer
Dim strComputer As String
Dim oWMI As Object, colFolders As Object, oFolder As Variant
Dim intCount As Integer
strComputer = "."
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = oWMI.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & rootFolder & "'} " _
        & "WHERE AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent", , 48)

For Each oFolder In colFolders
    intCount = intCount - (oFolder.Name Like wildCard)
Next
getFolderCount = intCount
End Function

example:
Code:
dim cntX as integer, cntY as integer
cntX = getFolderCount("C:\Location\Indexed", "*X")
cntY = getFolderCount("C:\Location\Indexed", "*Y")
 

Users who are viewing this thread

Top Bottom