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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Sunday, September 05, 1999

Using the FileSystemObject for Web Site Maintenance


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:

- continued -

'Declare our FileSystemObject variable Dim objFSO Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

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 FileSystemObject's GetFolder method to get our web's root folder.

'Declare our folder variable Dim objFolder 'Now, what is our web's root folder? Dim strRootFolder strRootFolder = "C:\InetPub\wwwroot\" 'set our folder object to the web root folder Set objFolder = objFSO.GetFolder(strRootFolder)

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:

<% Response.Write(Server.MapPath("default.asp")) %>

This will show you your web's root directory in the form: C:\Inetpub\wwwroot\default.asp. (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:

'Declare a file object Dim objFile 'Now, use a for each...next to loop through the Files collection For Each objFile in objFolder.Files Response.Write objFile.Name & " (" & objFile.Size & " bytes)<BR>" Next

This will print out on the screen the name of each file in the folder specified by objFolder 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, in our For Each ... Next loop above we would simply add the lines:

'Now, use a for each...next to loop through the Files collection For Each objFile in objFolder.Files Response.Write objFile.Name & " (" & objFile.Size & " bytes)<BR>" 'Check to see if objFile is an MP3 If LCase(Right(objFile.Name, 4)) = ".mp3" then Response.Write "<B>MP3 File!</B><P>" End if Next

We could also check to see if a file was too large. Let's amend our loop once again:

'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 Response.Write objFile.Name & " (" & objFile.Size & " bytes)<BR>" 'Check to see if objFile is an MP3 If LCase(Right(objFile.Name, 4)) = ".mp3" then Response.Write "<B>MP3 File!</B><P>" End if 'Make sure the file isn't too big If objFile.Size > MaxSize then Response.Write "<B>File is too large!</B><P>" End If Next

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!

'What is the maximum size of a file in bytes? Const MaxSize = 50000 '50 KB 'Did we find any violating files? Dim bolViolators bolViolators = False '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>" 'We found a violator bolViolators = True 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>" 'We found a violator bolViolators = True End If Next 'Now, if we didn't have any violating files, say so! If Not bolViolators then Response.Write "No violating Files..." End if

We used a new property of the File object in the above code, the Path property. The Path property prints out the full path of the file. So, an offending MP3 file's path in our root web directory might be: C:\InetPub\wwwroot\MyFirstMP3File.mp3.

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!!

Happy Programming!


  • Read Part 2


    Attachments:

  • Source code for above script in text format


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