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
205 active users
1218 visitors today
1036 pages today
(only part of today)
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

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


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

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?