Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 CoverYourASP --> Walking through server folders and files --> Part 4

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
17 active users
47 visitors today
172 pages today
how is this done?
Tools I use

CoverYourASP
Copyright © 1999-2012 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Walking through the folders

Now we want to walk through all the sub-folders in the folder. Another Folder collection called SubFolders has this list, so it's time for another Enumerator:

// now walk through sub-folders
var eSubFolder = new Enumerator ( oFolder.SubFolders );

while ( !eSubFolder.atEnd ( ) )
{
   var oSubFolder = eSubFolder.item ( );

   // recurse into sub-folder by calling myself
   WalkFolders ( oSubFolder, nLevel+1, fFolderNotify, fFileNotify );

   eSubFolder.moveNext ( );
}

In this loop we encounter another widely used method, recursion - a function calling itself until a condition is met.

Used wisely, recursion is a very powerful tool, but you must bear one thing in mind. Each time a function calls another, data is "pushed" onto the stack. When the function returns, that data is "popped" from the stack, and execution continues as before.

But, because the stack cannot grow past a certain size, uncontrolled recursion can easily "overflow the stack", and crash the application. Not good. Keep your local declarations few, and the levels of recursion small, and you'll be ok. It is unlikely (maybe impossible) that this code will ever overflow the stack because folders are rarely very deep.

You'll see that I pass nLevel+1 into the function. This is keeping track of how deep into the sub-folders I'm getting. You may have seen earlier that this is passed into the fFolderNotify and fFileNotify functions, so they can perhaps use this information.

Part 5: WalkFolders( ) in action...

Featured sponsor
My favorite resources

Tiki Statues - Tiki Masks - Tiki Totems



New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!