get lists names folders & subfolders and count the files extensions

tubrak

Board Regular
Joined
May 30, 2021
Messages
216
Office Version
  1. 2019
Platform
  1. Windows
hello

I search for macro to shows the folders names & subfolders names and count the files extensions

in column A it will bring the folders names and each folder contains subfolders as in column B it should bring it , each subfolders contains many files in differnt extensions pdf,avi,docx.... for instance the folder name RRT contains two subfolders name is JAN, DGH . subfolder JAN contain many files (pdf,jpj,docx)
and the same thing with DGH . should count how many in the subfolders . somtimes some folders contains many subfolders and somtimes there is no subfolder at all in the folder . with considerin when press cells in column A,B I can open it
FILES.xlsm
ABC
1folder namesubfolder nameextensions files counts
2RRTJAN5 PDF
33 JPG
41 DOCX
5DGH2 PDF
61 XLS
7RRNO SUBFOLDER3 XLS
8UPLOADIMAGES4 JPG
91 PNG
10INVNONO SUBFOLDER30 PDF
11VIDEOSNO SUBFOLDER15 MP4
12ACTION3 AVI
13
14
1
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Sure.

Had to change it up a bit on the setup for the original code.

Book1
ABCDEF
1FolderExtCountPath:C:\Users\ME\OneDrive\Desktop\Root
2Roottxt1
3Root\RootSub1txt1
4Root\RootSub2xlsx1
5txt2
6Root\RootSub2\RootSubSub1xlsx1
Sheet3


VBA Code:
Public Sub FileExtCount()
    On Error Resume Next
    Dim oFolder As Object, oSubfolder As Object, oFile As Object
    Dim TP As String, fExt As String
    Dim FSO As Object:          Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim SD As Object:           Set SD = CreateObject("Scripting.Dictionary")
    Dim AL As Object:           Set AL = CreateObject("System.Collections.ArrayList")
    Dim queue As Collection:    Set queue = New Collection
    Dim SkipNum As Integer:     SkipNum = 4
    Dim r As Range:             Set r = Range("A2")
    Dim path As String:         path = Range("F1").Value2
       
    queue.Add FSO.GetFolder(path)

    Do While queue.Count > 0
        Set oFolder = queue(1)
        queue.Remove 1
        For Each oSubfolder In oFolder.SubFolders
            queue.Add oSubfolder
        Next oSubfolder
       
        TP = TRIMPATH(oFolder.path, SkipNum)
       
        For Each oFile In oFolder.Files
            fExt = Split(oFile.Name, ".")(1)
            If Not SD.exists(fExt) Then
                SD.Add fExt, 1
            Else
                SD(fExt) = SD(fExt) + 1
            End If
        Next oFile
       
        For j = 0 To SD.Count - 1
            If j = 0 Then
                AL.Add Join(Array(TP, SD.keys()(j), SD.items()(j)), "*")
            Else
                AL.Add Join(Array(vbNullString, SD.keys()(j), SD.items()(j)), "*")
            End If
        Next j
       
        SD.RemoveAll
    Loop

Set r = r.Resize(AL.Count)

With r
    .Value = Application.Transpose(AL.toArray)
    .TextToColumns , DataType:=xlDelimited, Other:=True, OtherChar:="*"
End With

End Sub

Function TRIMPATH(s As String, n As Integer)
Dim SP() As String:     SP = Split(s, "\")
Dim RS() As Variant:    ReDim RS(1 To UBound(SP) - n)
Dim Pos As Integer:     Pos = 1

For i = n + 1 To UBound(SP)
    RS(Pos) = SP(i)
    Pos = Pos + 1
Next i

TRIMPATH = Join(RS, "\")

End Function

And then you need to add a worksheet event handler like this.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r As Range:     Set r = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

If Not Intersect(r, Target) Is Nothing And Target.Cells.Count = 1 Then
    Dim path As String: path = Range("F1").Value2
    path = Left(path, InStrRev(path, "\"))
    If Target.Value = vbNullString Then Set Target = Target.End(xlUp)
    Shell "explorer.exe " & path & Target.Value2, vbNormalFocus
End If
End Sub

Seems to be working the way it should testing it out on this end.

I was having a problem running the code of lrobbo314. So, if you are using office 2021 you can change this line:

VBA Code:
            fExt = Split(oFile.Name, ".")(1)

For these lines:

VBA Code:
            temp_arr = Split(oFile.Name, ".")
            fExt = temp_arr(UBound(temp_arr))

This is necessary if you have name of files with some dots in the middle.
 
Upvote 0
I was having a problem running the code of lrobbo314. So, if you are using office 2021 you can change this line:

VBA Code:
            fExt = Split(oFile.Name, ".")(1)

For these lines:

VBA Code:
            temp_arr = Split(oFile.Name, ".")
            fExt = temp_arr(UBound(temp_arr))

This is necessary if you have name of files with some dots in the middle.
I thought about that, but the OP never mentioned a problem. But yeah, good correction for posterity sake. ?
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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