ASP Code

Display Directory Contents with ASP

This code will display all subfolders and files of whatever directory it's in. It uses the FileSystemObject to get the directory contents, and displays each item as a link. This is a perfect solution for building an image gallery with ASP.

By default this script will not display any hidden folders. If you want to change that, take out the if statement that looks for Attribute 18. If you want this script to work recursively to display the contents of subfolders, just save it as 'default.asp' in each folder and subfolder.


<%
dim objFSO, strFile, objFile, objFiles, objFolder, objSubFolders, subfolder, file

set objFSO = Server.CreateObject("Scripting.FileSystemObject")
strFile = Request.ServerVariables ("PATH_TRANSLATED")

set objFile = objFSO.GetFile(strFile)
set objFolder = objFile.ParentFolder
set objSubFolders = objFolder.SubFolders
set objFiles = objFolder.Files

response.Write("<p>Directory contents as of " & now() & "</p>")

For each subfolder in objSubFolders
    if subfolder.Attributes <> 18 then
        Response.Write("<a href=""" & subfolder.Name & """>" & subfolder.Name & "</a><br />")
    end if   
next

response.write("<hr>")

For each file in objFiles
    if file.Attributes <> 18 then
        Response.Write("<a href=""" & file.Name & """>" & file.Name & "</a><br />")
    end if
next

set objFSO = nothing
set strFile = nothing
set objFile = nothing
set objFolder = nothing
set objSubFolders = nothing
set objFiles = nothing
%>
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image