ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Walking through server folders and files

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
24 active users
24 visitors today
25 pages today
(only part of today)
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!

As my site grew, more and more people asked me for the ability to search through the articles. With the articles spanning nearly 200 asp pages after being live for just 6 months it was definitely time!

The first step was to index the content in the files I had, so I wrote a function to walk through the files and folders using the FileSystemObject. I had almost finished before I realized that the function would be far more useful (for you, really, not me), if it could be used for a variety of purposes, not just indexing content.

So, I moved the function into a new Server Side Include, called WalkFolders.asp (in the utils folder of course). This article will step through the code, and end up with a demonstration walking through some test folders on the site (and in the download too incidentally, so you can play at home).

Calling the WalkFolders function

To make the function generic I wanted to pass in a starting folder, and two functions to be called when a new folder or file was found.

Let's look at an example of how to call it:

// create handle to FileSystemObject
var oFSO = Server.CreateObject ( 'Scripting.FileSystemObject' );

// start the search from our root folder
var oFolder = oFSO.GetFolder ( Server.MapPath ( 'WalkFolders' ) );

// start walking through the files and sub-folders
WalkFolders ( oFolder, 0, DisplayFolderName, DisplayFileName );

The first line creates an instance of the FileSystemObject that we are going to use. The second uses the FileSystemObject.GetFolder method to get a Folder object that can access the "WalkFolders" folder that I created as a test case. Note the use of Server.MapPath to turn the relative path into an absolute path on my server.

Lastly, we call my new function, WalkFolders( ). The parameters are the Folder object; the depth of the subfolder we're in (more about that later); and two functions that will be called by WalkFolders( ) whenever it encounters a new sub-folder or file.

Didn't realize you could pass functions around just like variables? It's a very powerful tool, and incidentally one of the reasons I use JavaScript - it's very close to C++.

Part 2: Inside WalkFolders( )...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



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...


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!