ASP Directory ListingTry the demo or view the source code.
This example shows how to read a directory structure in ASP. The demo lists the contents of a few directories located on this site, including any subdirectories and their contents, along with some information on file sizes and last modified dates.
ASP provides access to files on the host server via the
FileSystemObject. It is a generic object that can be used to
access existing files and folders or to create new ones.
It also provides information on drives, paths and special file types but the focus for this example is on file and folder objects.
File access permissions will apply to the script execution, so the user account which it runs under (such as an anonymous ID set up for web users) must have proper permissions to the files and directories accessed.
Other access permissions may be applied as well, such on Microsoft IIS web servers. Check your system documentation.
The Folder Object
To access a folder, you first create a
GetFolder() method specifying the physical path of that
<% set fs = CreateObject("Scripting.FileSystemObject") set folder = fs.GetFolder("C:\temp") %>
To find the path of a file on the local web site, the built-in
Server.MapPath() function can be used to translate a URL to a
<% path = Server.MapPath("/about/") %>
Which might return "C:\Inetpub\wwwroot\about\" for example. This value can
then be used in the
Folder object's properties are the
SubFolders collections. These can be enumerated to list the
The demo script does just this, generating a display
for each file and subfolder along with some information on file size, last
modified date, etc. using a subroutine called
<% sub ListFolderContents(path) dim fs, folder, file, item, url set fs = CreateObject("Scripting.FileSystemObject") set folder = fs.GetFolder(path) 'Display the target folder and info. Response.Write("<li><b>" & folder.Name & "</b> - " _ & folder.Files.Count & " files, ") if folder.SubFolders.Count > 0 then Response.Write(folder.SubFolders.Count & " directories, ") end if Response.Write(Round(folder.Size / 1024) & " KB total." _ & "</li>" & vbCrLf) Response.Write("<ul>" & vbCrLf) 'Display a list of sub folders. for each item in folder.SubFolders ListFolderContents(item.Path) next 'Display a list of files. for each item in folder.Files url = MapURL(item.path) Response.Write("<li><a href=""" & url & """>" _ & item.Name & "</a> - " _ & item.Size & " bytes, " _ & "last modified on " & item.DateLastModified & "." _ & "</li>" & vbCrLf) next Response.Write("</ul>" & vbCrLf) end sub %>
Note the recursive call the subroutine makes to itself for each subfolder. This allows all the entire file hierarchy to be traversed and displayed from whatever starting path is given.
In order to make hypertext links to each file, the physical file path has to
be mapped to a URL, just like
Server.MapPath() converted a URL to
a physical path. Unfortunately, there is no built-in function for this.
MapURL() function is defined to map a physical
file path back to a URL.
<% function MapURL(path) dim rootPath, url 'Convert a physical file path to a URL for hypertext links. rootPath = Server.MapPath("/") url = Right(path, Len(path) - Len(rootPath)) MapURL = Replace(url, "\", "/") end function %>
It simply takes the physical path of the web site root directory and strips it from the given path. Then any backslash characters are replaced with forward slashes.