Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through files - make recursive!? Help please 2

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
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.

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
 
Something like this ?
Code:
Sub LoopFoldersListFiles(path As String)
Dim fso As New FileSystemObject
Dim f As Folder, sf As Folder, fi As File
    Set f = fso.GetFolder(path)
    For Each sf In f.SubFolders
         LoopFoldersListFiles(sf.Path)
    Next
    For Each fi In f.Files
        Debug.Print fi.Path
    Next
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the quick reply PHV. I had been working on something similar, but it only seems to go to one extra level of subfolder, not to the fullest extent.

My original code goes 2 levels deep, adding your extra code goes 3 levels deep. I cant work out how to get it completely recursive and go infinitely deep...

I think the problem code seems to be in my first coding
Code:
FileName = Dir$()
 
Have you really tested my code ?
The procedure I posted has NO level limitation.
 
BTW, I guess the limitation is the immediate window buffer size ...
 
Hi PHV. I did test your code, which as I say would go to 3 levels. I don't understand your next reply, are you suggesting the immediate window is too small to display the depth?

I just tried setting up some test folders 5 levels deep with 2 files in each folder (so that it will display all results OK) and it still only goes 3 levels deep.

Like I say, maybe the problem is with my first code?
 
PHV's code is completely recursive. If you run the code as he has written it, you will see a complete list of all the files all the way down a directory tree in the immediate window. If you have integrated his sample code into your code in some fashion, then you will need to post the code that you are actually running.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I did just copy his code back - note that his code references my original code in itself - and I think that is what breaks it - the use of dir$()

I have done some more searching and found some alternative code on this site that uses MS Scripting Runtime and appears to work fully. This obviously doesnt use dir$()

Thanks for your help guys.

Code:
Function cmdGetFiles_Click()
   
   WalkDirTree "C:\Access\"

End Function

Private Sub WalkDirTree(TopDir As String)
    
   Dim FSO As FileSystemObject
   Dim FileList As New Collection
   Dim Idx As Integer
   
   Set FSO = CreateObject("Scripting.FileSystemObject")
   ProcessFolder FSO.GetFolder(TopDir), FileList
   Set FSO = Nothing
   
' Here is where you process all of the found files

   If (FileList.Count > 0) Then
      For Idx = 1 To FileList.Count
'         MsgBox "Process File:  " & FileList.Item(Idx)
         Debug.Print "Process File:  " & FileList.Item(Idx)
      Next Idx
   Else
      MsgBox "No Files Found"
   End If
   
End Sub

Private Sub ProcessFolder(FolderName As Scripting.Folder, FileList As Collection)

   Dim SubFolders As Scripting.Folders
   Dim FileNames As Scripting.Files
   Dim SubFolderName As Scripting.Folder
   Dim FileID As Scripting.File
   
   Set SubFolders = FolderName.SubFolders
   For Each SubFolderName In SubFolders
      ProcessFolder SubFolderName, FileList
   Next
   
   Set FileNames = FolderName.Files
   For Each FileID In FileNames
         FileList.Add FileID.path
   Next
   
   Set FileID = Nothing
   Set SubFolderName = Nothing
   Set FileNames = Nothing
   Set SubFolders = Nothing
   
End Sub
 
Hi PHV. I did test your code
By COMPLETELY replacing your sub with mine ?
 
Hi PHV! You are right, I am wrong! It was very late here when I was posting and trying code.

I did copy your sub completely but then changed the name. Then when you sub becomes recursive it was calling my old sub, because I changed the name of your sub.

I have tried your code PROPERLY and it works - so heres a star! Sorry for being dim.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top