There are many threads on here about searching through subfolders of a folder, but I can't seem to find any that mention how to search through folders of that subfolder. My following code searches through the subfolders of a folder, but sometimes my subfolders have folders themselves that I need to search through as well, but my current code does not access them. Can someone please help?
Public Sub count_files()
Dim sht As Worksheet
Dim path As String
Dim FSO As New FileSystemObject
Dim obj_folder As Object
Dim subfolder As Object
Dim subfolders As Object
Dim file As Object
Dim file_count As Long
Dim i As Long
Set sht = ThisWorkbook.Sheets(1)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.TransitionNavigKeys = False
path = "\\somepath\xxxxx\somepath\xxxxxx\somepath\xxxxx\somepath\xxxxx\somepath\"
Set FSO = CreateObject("Scripting.filesystemobject")
Set obj_folder = FSO.GetFolder(path)
Set subfolders = obj_folder.subfolders
file_count = subfolders.Count
Debug.Print file_count
i = 2
For Each subfolder In obj_folder.subfolders
For Each subfolder In obj_folder.subfolders
For Each file In obj_subfolder.Files
If file.Name Like "*.xlsb*" Or file.Name Like "*.xlsx*" Or file.Name Like "*.xlsm*" Then
sht.Cells(i, 1) = file.Name
sht.Cells(i, 2) = obj_subfolder.Name
i = i + 1
End If
Next file
Next subfolder
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub