I have a loop which runs through the files in a folder and subfolder. I need this to be recursive (so it runs through all subfolders until they are all done).
I have never done any recursive coding so would really appreciate being shown how! Or if there is better code then I am using, please make any suggestions.
I have never done any recursive coding so would really appreciate being shown how! Or if there is better code then I am using, please make any suggestions.
Code:
Sub LoopFoldersListFiles(path As String)
'Loops thru sub folders, listing files
'Application.ScreenUpdating = False
Dim fso As New FileSystemObject
Dim f As Folder, sf As Folder, r As Integer, fname As String
'Get a reference to the Folder object.
Set f = fso.GetFolder(path)
'Iterate through subfolders.
r = 1
On Error Resume Next
For Each sf In f.SubFolders
fname = path & sf.name & "\*.*"
FileName = Dir$(fname)
Do While FileName <> ""
Debug.Print path & sf.name & "\" & FileName
r = r + 1
FileName = Dir$()
Loop
Next
End Sub