ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 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
157 active users
3155 visitors today
4718 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

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

ASP.NET Blog
ASPRSS
ShawThing

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

Selling products online - read how James does it now



Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...