The situation: you allow users to upload files to your web server. Perhaps you run a site like GeoCities, and offer free home pages. You'd like to make sure people weren't abusing the free home pages system by using them as MP3 servers. So, you'd like to have a nifty script to search through the web site looking for MP3 files or files over a certain size (let's say 50KB). You can create such a script using the powerful FileSystemObject. The FileSystemObject, according to Microsoft, "Provides access to a computer's file system." It does this with the help of a few objects: the Drive object, the Folder object, and the File object. With these three objects, you can do just about anything to the web server's file system that you could do with the command prompt. You can move files, delete files, determine attributes of files, determine how much free space you have available on a drive, and much more. Let's start by looking at how we'd list the files in a given directory. First things first, you will want to create an instance of the FileSystemObject in your code:
That's all you need to do to create an instance of the FileSystemObject. Pretty easy so far, eh?
Next we need to declare a variable that will serve as our folder object, and then we need to use the
GetFolder method to get our web's root folder.
You may be wondering how I knew my root web's directory was
C:\InetPub\wwwroot\. Well, one
easy way to find out is to write the following script in your root web folder:
This will show you your web's root directory in the form:
(Or whatever your root web is, the point is, it will show the
default.asp part as well.)
Just ignore the
default.asp at the end of the path, and just use the full directory name
(in this case,
C:\Inetpub\wwwroot) as your web's root directory.
Each directory object has a property called
Files, which is a collection of File objects.
This, not surprisingly, represents the list of files in the current directory! So, if we wanted to list
all of the files in the folder specified by
objFolder, we'd just continue our script with:
This will print out on the screen the name of each file in the folder specified by
and the file's size in bytes. Now, since we want to determine if the file is an MP3 or not, we are going
to be interested in it's extension. To determine a file's extension, we can use a bit of string magic!
You've gotta love the string functions inherent in VBScript, they are so useful. Basically what we want
to check for is a string who's last four characters are
.mp3. So, to check for this,
For Each ... Next loop above we would simply add the lines:
We could also check to see if a file was too large. Let's amend our loop once again:
Finally, we don't want to list all of the files, just the files that violate either the size constraint
or have the
.mp3 extension. So, let's amend this loop one last time!
We used a new property of the File object in the above code, the
Path property. The
property prints out the full path of the file. So, an offending MP3 file's path in our root web directory
Does that wrap everything up? Well, kind of. The disadvantage of this script is that is searches only one directory. Wouldn't it be nice to have a script that searches every directory in the entire web site? Well, it would, but that is going to have to wait for another article. Performing such a search requires a bit of knowledge about recursion, so if you are unfamiliar with recursion, I strongly suggest you read this article on recursion: Recursion: Why It's Cool.
In Part 2, we use recursion to iterate through the entire directory structure!
I hope you find this script useful, and, above all, I hope you learned something new and had fun!!