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 functionTo 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( )... |