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

How deep can you nest recursive calls based on FileSystemObject Drive

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
I am using the FileSystemObject and spinning through the Drives collection and then processing the Subfolders and Files collections with code similar to the following. How many levels can you nest with a recursive call. I don't want to blow the call stack and I don't know in advance how many levels of subdirectories a given client user may have set up.

Thanks in advance for any help and/or suggestions!

'Skip A drive and other removable drives
If drv.IsReady Then
For Each fld In .RootFolder.SubFolders
If Not fld Is Nothing Then

Call ProcessFolder(fld)

End If
Next fld
End If

Private Sub ProcessFolder(pfldFolder As Folder)
Dim fld As Folder
Dim fil As File
On Error Resume Next
'Process any files for this folder
For Each fil In pfldFolder.Files
'Do some processing
Next fil

'Make recursive call with subfolder collection
For Each fld In pfldFolder.SubFolders
Call ProcessFolder(fld)
Next fld

Set fld = Nothing
Set fil = Nothing
On Error GoTo 0
End Sub 'ProcessFolder



 
You can use something like this to test it on your machine:
[blue]
Code:
Dim Counter As Integer

Sub TestRecursion()
  Counter = Counter + 1
  If Counter < 3000 Then
    TestRecursion
  Else
    MsgBox Counter
  End If
End Sub
[/color]

I ran out of stack space somewhere between 3000 and 4000.
(Excel 2K on Win98)
 
It is worth noting that many of the underlying Windows API calls only support a maximum path length of 260 characters (including the drive letter spec - <x>:\ - and a trailing null, thus actually only allowing 256 characters for the path itself) and that, whilst some of the Unicode API's allow for a path length of 32767 characters assuming an average path element length of 8 characters means maximum path depths in either case of 32 and just under 5000 respectively.

In the first case, you should have no worrys about the recursion overflowing. In the second - well, anyone who has files buried 5000 levels down is just asking for trouble...
 
Thanks to both of you for your quick and timely responses. That's what I love about this board, good answers quickly even on holiday weekends!

Have a great day!

 
What a coincidence. I was working on something very similar and your post helped me solve it. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top