0

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
braX
  • 11,506
  • 5
  • 20
  • 33
AP1
  • 157
  • 1
  • 4
  • 14
  • 2
    You are looking to search thru them recursively... – braX Jan 16 '20 at 20:36
  • @braX, it wasn't really helpful for me only because I wasn't sure how to implement it into my code. I tried a few things and none of them pulled every file. – AP1 Jan 17 '20 at 00:41

0 Answers0