ASP Script to list the Contents of a Folder

The script below lists the contents of a folder (directory) in the form of a web page. The advantage is that it can work as an index file that automatically updates itself. That is, if you add files and subfolders to the folder containing this script, they will immediately appear as links in the generated web page. The link text for each file and subfolder is the filename itself.

Cut and paste the script below as-is into an empty .ASP file, then change the strings at the top of the script as appropriate. Place the resultant ASP file into the relevant folder, and it'll generate a listing of that folder as a web page.

It's free to use, but I offer no warranties of any kind. Bear in mind that listing all the contents of a folder may not be a good idea from a security point of view - check with your web site's administrator.

See the code in action

<%@ Language=VBScript%>
'List the contents of the current
'***** Enter a BOOKMARK TITLE for this web page in the line below ***
strTitle = "Title goes here"
'***** Enter a MAIN HEADING for this web page in the line below *****
strHeading = "Heading goes here"
'You don't need to change anything below this line
<%response.write ("<TITLE>" & strTitle & "</TITLE>")%>
    response.write ("<H1>" & strHeading & "</H1>")
    set FileSysObj=CreateObject("Scripting.FileSystemObject")
    strFileAndPath = request.servervariables("SCRIPT_NAME")
    strPathOnly = Mid(strFileAndPath,1 ,InStrRev(strFileAndPath, "/"))
    strFullPath = server.mappath(strPathOnly)
    set fldr=FileSysObj.GetFolder(strFullPath)
    response.write("<H2>Folders list</H2>")
    set FolderList = fldr.SubFolders
    For Each FolderIndex in FolderList
        Response.Write("<A HREF='" & & "'>" & & "</A><BR>")
    response.write("<H2>Files list</H2>")
    set FileList = fldr.Files
    For Each FileIndex in FileList
        'This bit excludes this page (and other asp files) from the list of links
        if Lcase(right(FileIndex.Name, 4)) <> ".asp" then
            Response.Write("<A HREF='" & & "'>" & & "</A><BR>")
        end if

Home About Me
Copyright © Neil Carter

Content last updated: 2005-06-20