Getting 2nd level of subfolders

MM91

Board Regular
Joined
Nov 29, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
Hi I have this code that I am using to list the names of subfolders in an array. However I need it to go one level deeper and get the subfolders of all these folders. What is the best way of doing this? Every method I use goes multiple levals deep and i am left with a list of all folders.

Dim i As Integer
Dim m As Integer
Dim serialnosf() as string
dim NumSF as integer

SerialNoFP = "C:\Users\mmi\Desktop\New folder\test"

Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(SerialNoFP)
Set sf = f.SUBFOLDERS
NumSF = f.SUBFOLDERS.Count
i = 1
ReDim SerialNoSF(1 To NumSF)
For Each f1 In sf
s = f1.Name
SerialNoSF(i) = s
i = i + 1
s = ""
Next


For m = 1 To NumSF
SerialNoUF.SerialNoBox.AddItem SerialNoSF(m)
Next
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
There is a bunch of ways you could do it, but here is one:

VBA Code:
Dim TempSheet As Worksheet
Dim F As Object, FSO As Object, SF As Object, RF As Object
Dim RowNumber As Long: RowNumber = 2
Dim SerialNoSF As Variant

Const SerialNoFP = "C:\Users\mmi\Desktop\New folder\test"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set RF = FSO.GetFolder(SerialNoFP)
Set TempSheet = ThisWorkbook.Sheets.Add
TempSheet.Range("A1:B1") = Array("Folder", "Subfolder")

For Each F In RF.Subfolders
    For Each SF In F.Subfolders
        TempSheet.Cells(RowNumber, 1).Resize(, 2) = Array(F.Name, SF.Name)
        RowNumber = RowNumber + 1
    Next SF
Next

SerialNoSF = TempSheet.UsedRange
Application.DisplayAlerts = False
TempSheet.Delete
Application.DisplayAlerts = True
 
Upvote 0
Another option
VBA Code:
Dim i As Integer
Dim m As Integer
Dim serialnosf() As String
Dim NumSF As Integer

SerialNoFP = "C:\Users\mmi\Desktop\New folder\test"
ReDim serialnosf(1 To 10000)
Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(SerialNoFP)
Set sf = f.Subfolders
i = 1
For Each f1 In sf
   serialnosf(i) = f1.Name
   i = i + 1
   For Each f2 In f1.Subfolders
      serialnosf(i) = f2.Name
   i = i + 1
   Next
Next
ReDim Preserve serialnosf(1 To i - 1)

SerialNoUF.SerialNoBox.List = serialnosf
 
Upvote 0
Another option
VBA Code:
Dim i As Integer
Dim m As Integer
Dim serialnosf() As String
Dim NumSF As Integer

SerialNoFP = "C:\Users\mmi\Desktop\New folder\test"
ReDim serialnosf(1 To 10000)
Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(SerialNoFP)
Set sf = f.Subfolders
i = 1
For Each f1 In sf
   serialnosf(i) = f1.Name
   i = i + 1
   For Each f2 In f1.Subfolders
      serialnosf(i) = f2.Name
   i = i + 1
   Next
Next
ReDim Preserve serialnosf(1 To i - 1)

SerialNoUF.SerialNoBox.List = serialnosf
this worked great thank you so much!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,851
Members
449,194
Latest member
HellScout

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