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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through sub folders 2

Status
Not open for further replies.

ErroR

Technical User
Apr 9, 2001
79
US
I'm using this to loop through each file in a directory which I specify:
Code:
  Dim FSO As FileSystemObject
  Set FSO = New FileSystemObject
  Dim FSOFolder As Folder
  Dim FSOFile As File
    
  Set FSOFolder = FSO.GetFolder(mstrFolderSpec)
  
  For Each FSOFile In FSOFolder.Files
    'set file attributes, copy files, whatever
  Next
But I want to be able to do a "For Each (Folder instead of File) so that I can then get inside each sub folder in the one parent folder that I've specified. How do I accomplish this so that I can do For Each Folder, then call the For Each File procedure?

Thanks
 
Hi ErroR

Think this should do what you need. If you want to view subfolders within sub folders it get a little more complex.

Set SubFolders = FSOFolder.SubFolders

If SubFolders.Count <> 0 Then
For Each SUBFOLDER In SubFolders
Debug.Print SUBFOLDER
Next
End If


Stew &quot;Even a stopped clock tells the right time twice a day.&quot;
 
Hey thanks mymou...

I never thought about it being more complex, but I'm going to need to loop through every subdirectory and their subdirectories as well!

What's the easiest way to accomplish that? Some of these will be nested 10 folders deep...

And the FSO object has always escaped me.. you're doing a Set command on &quot;SubFolders&quot;.... what object type should that be Dim'ed as?
 
Dear ErroR,

seems to me a typical recursion problem.

you need to create a function which takes the folder you want to inspect as argument.

like this

private function domyfolderthing(curfolder as folder)

dim subfolder as folder
if curfolder.subfolders > 0 then

for each subfolder in curfolder.subfolders

domyfolderthing subfolder
next subfolder
end if


debug.print curfolder.name
//or whatever you want do to every folder you find on your way down

end function

I did not cross-check the code, so if you have any problems let me know


regards Astrid
 
I'm still having troubles getting deeper than just one Sub-Folder for each time I do a For Each SubFolder statement...
 
dear ErroR,


this works fine on mine


Public Sub trysubfolders()
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
Dim FSOFolder As Folder

'I have a folder for my projects called c:\projekte so I take this for example
Set FSOFolder = FSO.GetFolder(&quot;c:\projekte&quot;)
'here is the first call of the recursive function
domyfolderthing FSOFolder

End Sub

Private Function domyfolderthing(curfolder As Folder)

Dim subfolder As Folder

If curfolder.subfolders.Count > 0 Then 'forgot count before

For Each subfolder In curfolder.subfolders
'here the function call itself
domyfolderthing subfolder
Next subfolder
End If


Debug.Print curfolder.Name
'or whatever you want do to every folder you find on your way up

End Function


Just change the folder I used into a folder with sub and subsubfolders and try it.
If it still does not work post your changed code please.

HTH
regards Astrid
 
Whoa... that works, but now what grasp I thought I had on VB flow just went out the window... I guess I didnt think you could use a function to call itself like what this is doing. Recursive...

Thanks Astrid, thanks for the detailed explanation... I don't think I would have ever gotten it without the details. However now I've learned why I've seen warnings about recursive function calls... wow I was able to really make some interesting errors....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top