ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

FAQ for the Directory List sample:

I just want a list of the files in a directory. Your script has all these functions, tables, and images and confuses the heck out of me. Can you make this any simpler?

I don't see why not... check out this simpler version: dir_list_simple.asp.

Same as above, but I want the files listed in a select box of my form.

Again... a perfectly reasonable request. Here's a script that uses the same directory as a source to fill up a select box. The form involved doesn't do much, but it could be modified to do whatever you'd like it to.

Anyway... check out dir_list_form.asp. It's relatively straight forward and most of the code is the same as the simple version above.

I'm getting a "Path not found" error. Is this a security issue?

It's not usually security related. Our sample uses "dir" which when run through Server.MapPath evaluates to the dir folder underneath the current location. You probably don't have a directory named this on your file system.

You can set it to any virtual path (ie. "folder", "/folder/", etc.) that exists on your site and the MapPath command will figure out the appropriate physical location or you can specify a physical location (ie. "c:\folder", "c:\folder\folder") and remove the call to Server.MapPath.

How do I display the file sizes with commas between every third digit for numbers 1,000 and larger to make the numbers easier to read?

While you could always write a function to add the commas yourself, luckily you don't need to. Recent versions of VBScript already include the FormatNumber function. This function takes a number, applies the specified formatting, and returns the result. The basic syntax looks like this:

FormatNumber(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])

To add the commas as you requested, you would call the function like this:

FormatNumber(objItem.Size, 0, , , True)

If you'd like to learn more about the FormatNumber function, the following links may be of interest.

and for more on VBScript in general:

I'm dealing with large files and I'd like to display the file size in kilobytes (Kb) instead of bytes. How do I modify the script to do that?

The good news is that it's actually quite simple. The bad news is that things will look weird unless we manipulate them a little. In order to display file sizes as Kb instead of bytes all we need to do is divide the file size by 1,024 (the number of bytes in a Kilobyte). So instead of this line (which appears in the original script):

  <td align="right"><%= objItem.Size %></td>

We'd use a line that looks like this:

  <td align="right"><%= objItem.Size / 1024 %></td>

The problem we run into is that very few file sizes are evenly divisible by 1,024 and so we end up with a lot of not so significant digits. Do you really care that a 11,721 byte file is 11.4462890625 Kbytes? To make things look relatively normal, you'll probably want to round off your results. Luckily that's fairly easy to do as well. The following line will convert to Kb and round the result to two decimal places.

  <td align="right"><%= Round(objItem.Size / 1024, 2) %></td>

You can change the number of decimal places by changing the second parameter of the Round function. Oh and don't forget to change the label in the heading from "bytes" to "Kilobytes" or people will be relatively confused when you tell them the file is 11.45 bytes.

Is there any easy way to order the files by date, type, size, etc.?

The short answer is no... there's no easy way to do it. The longer answer is that yes... it can be certainly be done, but it's not nearly as easy as it should be.

There are a couple ways to accomplish it, but none are really very elegant. The bottom line is that you need to find some other way to do the sorting since the FileSystemObject won't do it for you.

Because it's not that easy, in order to help everyone out I've written up a new sample that does this using just one of the many possible methods. The method I chose was to insert the file information involved into an ADO recordset and then use the built in recordset sorting capabilities. You can take a look at the code by checking out our Directory List (Sorted) Sample.

As I mentioned... this is just one approach. It incurs some overhead since you're creating additional objects, but the code is simpler then writing the sorting routine yourself would be and therefore leaves less room for error.

Can I use your script to filter or sort files and still make it appear to users as though they are seeing the files via IIS's directory browsing feature?

Yes and no. While you can't emulate IIS's format exactly, and an experienced user will still be able to tell the difference, you can get pretty darn close.

This scenario can be quite handy for filtering certain files out of the list, but still providing users with the same familiar interface they may be used to seeing elsewhere. The key is to set the directory browsing script as the default document in the directory. You can usually accomplish this by simply naming the script "default.asp" although some hosts use "index.asp" instead.

I've done my best to emulate the format that IIS uses as closely as possible, but you should obviously feel free to change things to better fit your needs.

I've added the script to the sample folder that we use to illustrate our other directory display samples. You can view it in action here:

For those of you that prefer, I've also created an ASP.NET version. You can view it in action here:

The source code for both versions is available for download from here: (3.4 KB)

Your sample shows how to get the time and date a file was created, but how do I get the time and date a file was last modified?

If you're using a recent version of the scripting libraries, it's actually just as easy as getting the created date. Instead of:


you simply use:


The problem comes if you're running an older version. I'm not sure exactly when it was added, but early versions didn't have the DateLastModified property. Not to worry... we've got a Last Modified sample from way back when that explains the whole thing.

Was your question not answered above?

Ask a new question

Please note: This form is only for submitting questions about the sample for us to consider including in the FAQ. If we feel the question merits inclusion, we will include it along with a reply. We will not respond to your email individually.

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers