Published: Wednesday, February 02, 2000
Generating a Web Site File System Report, Part 2
Read Part 1
In
Part 1 we discussed the overview of the Web Site File System Report.
To summarize, the reporting application consists of three ASP pages. The first ASP page,
ExamineWeb.asp
displays a table containing the various file extensions found on the Web site. Also included are numbers showing
the total number of files with a particular extension, how many bytes the files with the extension take up,
and percentages. Also recall that the work horse of
ExamineWeb.asp is the function
TraverseFolder.
Let's begin by looking at the code for ExamineWeb.asp:
<% Option Explicit %>
<!--#include file="BarChart.asp"-->
<%
sub TraverseFolder(objFolder, _
objDict, _
objSizeDict, _
bolRecurseDirectories)
'Only process directories that do NOT start with
'an underscore.
If Not Left(objFolder.Name,1) = "_" then
Dim objFile, strPath, strExtension
'Iterate through each file in the folder
For Each objFile in objFolder.Files
'Obtain the extension of the current file
strPath = objFile.Path
strExtension = Ucase(Right(strPath, Len(strPath) - _
InStrRev(strPath, ".")))
'Add it to the dictionary if needed
If Not objDict.Exists(strExtension) then
objDict.Add strExtension, 1
objSizeDict.Add strExtension, objFile.Size
Else 'increment the dictionary value
objDict(strExtension) = objDict(strExtension) + 1
objSizeDict(strExtension) = objSizeDict(strExtension) + _
objFile.Size
End If
Next
'Recurse through the folder's subdirectories if needed
Dim objSubFolder
If bolRecurseDirectories then
For Each objSubFolder in objFolder.SubFolders
TraverseFolder objSubFolder, objDict, _
objSizeDict, bolRecurseDirectories
Next
End If
End If
end sub
|
Note that at the top of ExamineWeb.asp we include a file called BarChart.asp. I will
not be discussing the specifics of this file. A set of previous 4Guys articles delves into the topic of
creating dynamic bar charts, and can be found at: Making Bar Charts Using ASP.
I will, however, provide the code I used for BarChart.asp at the end of this article.
The function TraverseFolders begins with a quick check to see if the folder's name begins with
an underscore. Microsoft's FrontPage Extensions will create "hidden" directories in your Web site containing
a leading underscore. We don't want to traverse through these, so if we hit a folder with a starting underscore,
we'll skip it. (Note that this will only occur when we have selected to recurse through the root folder's
subdirectories.)
If the folder name does not contain a leading underscore, a For Each ... Next loop is
used to iterate through each file in the folder specified by objFolder. The file's extension is
obtained (through some string manipulation of the file's path). Next, we check whether or not a key already
exists in the dictionary object objDict using the Exists method.
Recall that the
key for both objDict and objSizeDict are the file extensions. So, if we encounter
a file that has an extension we've not seen before, it's added to the dictionary objDict with
an initial value of 1. Also, it's added to the objSizeDict dictionary, with an initial value
equal to file's size in bytes.
If we reach a file that has the same extension as a file we've previously visited (that is, a key with
the same value as strExtension already exists in the dictionary), then we want to increment
the value of that particular key in objDict by 1, and increment the value of that particular key
in objSizeDict by the file size of the file just visited.
Once we've iterated through all of the files in the folder specified by objFolder,
objDict will contain a key for each unique file extension, and each key's value will contain the
number of files that contain that extension. Similarly, objSizeDict will contain a key for each
unique file extension, and each key's value will contain the total bytes consumed by that particular extension.
Only one step remains. If the user wants to recurse directories, then we need to iterate through the current
folder's subfolders and recursively call TraverseFolder. If you are unfamiliar with
recursion, I highly recommend you learn more about it before continuing. There is a good article on 4Guys
that discusses what, exactly, recursion is and how it can be used:
Recursion, Why It's Cool. You can also read
Using the FileSystemObject for Web Site Maintenance, which
has a thorough discussion on using FSO to recursively iterate through a directory tree.
If you are unfamiliar with the Dictionary object, you should read Utilizing
the Dictionary Object in VBScript. I also highly suggest that you read the
technical documentation on
the Dictionary object. Finally, if you are not familiar with the FileSystemObject, you should
take a moment to read the FileSystemObject FAQ.
In Part 3, we'll look at the remainder of the code in
ExamineWeb.asp!
Read Part 3
Read Part 1