When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Tuesday, September 14, 1999

Using the FileSystemObject for Web Site Maintenance, Part 2

In Part 1 of Using the FileSystemObject for Website Maintenance, we demonstrated how to use the FileSystemObject to loop through a given directory to search for files that were either too large (over 50 KB) or were MP3 files. The one drawback to our script was that it only searched one directory. What if we wanted to search ALL the directories on our web site? This can be done using recursion. If you are unfamiliar with what recursion is, I strongly suggest you check out the article: Recursion, Why it's Cool!

- continued -

Since there are an undetermined number of subdirectories in a given directory, and since each subdirectory may contain its own set of subdirectories, which in turn may contain their own set of subdirectories, and so on and so on, it is imperative that we use recursion. You can think of directory structure as a hierarchy. At the top of the hierarchy is the current directory. Each level down on the hierarchy is the subdirectories of the level above it. For example, your web site might contain the following structure:

       |       |
    Images   scripts

This is a rather simple structure, of course. Imagine, however, that you had a very complex directory structure that changed often. Recall in Part 1, the scenario we discussed was that of a site that allowed visitors to create their own web pages, like GeoCities. If we allowed our users to also create directories within their web, we could have a very complex and immense directory structure. Such a complex directory structure must be picked apart through recursion.

So, how do we use recursion to step through the complete directory structure? Well, when using recursion, it is best to ask yourself a general question, such as, "What am I going to be doing at a given directory?" Well, first we'll want to process all the files of the given directory. Next, we need to repeat step one with all of the current directory's subdirectories. It may sound a bit confounding; hopefully a pseudocode example will clear things up a bit. Here is what we want to do:

    Step 1: Process the files of the current directory
    Step 2: With each subdirectory, apply step 1

Say we have the following directory structure:


We would start with Dir1 and process all of it's files. We would then move to step 2, and apply step 1 to all of Dir1's subdirectories. So, that takes Dir2 and applies step 1. Step 1 is to process all of the files in the current directory, which is Dir2. Step 2 is to apply step 1 to all of Dir2's subdirectories, which is Dir3. So, we apply Step 1 to Dir3, and process all of Dir3's files. Step 2, does not apply to Dir3, since Dir3 has no subdirectories, so we have finished. Note that we have processed every file in our directory structure. We could show a mathematical induction proof to prove that each file was processed, but that's beyond the scope of this article!

Now, let's write this recursive function!

Sub IterateThroughDirectory(objFolder) ' *** STEP 1 *** 'Process each file to determine if it is larger than 50kb 'or is of type MP3 'What is the maximum size of a file in bytes? Const MaxSize = 50000 '50 KB 'Now, use a for each...next to loop through the Files collection For Each objFile in objFolder.Files 'Check to see if objFile is an MP3 If LCase(Right(objFile.Name, 4)) = ".mp3" then 'Print out the violating file name and location Response.Write "<B>MP3 File!</B><BR>" Response.Write objFile.Path & "<P>" End if 'Make sure the file isn't too big If objFile.Size > MaxSize then 'Print out the violating file name and location Response.Write "<B>File is too large! (" & objFile.Size & " bytes)</B><BR>" Response.Write objFile.Path & "<P>" End If Next Dim objFile For Each objFile in objFolder.Files if Lcase(Right(objFile.Name,4)) = ".mp3" then objDict.Add CStr(iCount), objFile.Path iCount = iCount + 1 end if Next ' *** STEP 2 *** 'Now that we've processed files, repeat step 1 for all the subdirectories Dim objSubFolder For Each objSubFolder in objFolder.SubFolders IterateThroughDirectory objSubFolder Next End Sub

The above function will iterate through each file in a directory (step 1), then apply step 1 to all of the subdirectories of the current directory (step 2). The code shown above in Step 1 is taken directly from Part 1 of this article. To start this recursive function, you just need to pass it a valid Folder object. The below code will do just that:

Dim objFSO Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 'Create the Dictionary object we'll be using Dim objDict Set objDict = Server.CreateObject("Scripting.Dictionary") Dim strCurrentFolder strCurrentFolder = "C:\Inetpub\wwwroot\" Dim objFolder Set objFolder = objFSO.GetFolder(strCurrentFolder) IterateThroughDirectory objFolder

The last line above, IterateThroughDirectory objFolder, starts the recursion. In my opinion, this is freakin' cool. I hope you think it's neat too!

Happy Programming!


  • View the source code for the recursion function

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article