![]() |
|
|
Published: Tuesday, September 14, 1999
In Part 1 of Using the FileSystemObject for Website Maintenance, we demonstrated how to use the
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:
C:\INetPub\wwwroot
|
---------
| |
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 2: With each subdirectory, apply step 1 Say we have the following directory structure:
Dir1
|
Dir2
|
Dir3
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!
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:
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!
Attachments:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||