Display Directory Listing
First off let me say that this code is a modified version of Kaustav Acharya’s post on Planet Source Code. I simplified the code to better suit my needs. You should just be able to copy the code posted below, and in order for this code to work you MUST update the SELECT CASE statement and configure it to work on your server’s structure. I think the other code is a bit harder (not user friendly) to use/fix for inexperienced programmers. Once configured properly, this code will display the contents of a directory wherever this file is placed. I named the file to be inserted into the directory to display index.asp, just because my default web site is set up to read an index.asp file as the default web page. You can also get cute with this script as well, write another select case statement checking for the file types, and display the pertinent icon according to the file name. Not what I need this code for, but just a suggestion. Happy Coding!
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
<%@ Language=VBScript %>
<% Option Explicit %>
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Directory Listing</title>
<style type="text/css">
TABLE
{
BORDER-BOTTOM: 0px;
BORDER-LEFT: 0px;
BORDER-RIGHT: 0px;
BORDER-TOP: 0px;
FONT-FAMILY: tahoma,sans-serif;
FONT-SIZE: 12px
}
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="blue" vlink="blue">
<%
Dim strDirName, strMyPath, objFSO, strFolder, strFiles, intFileCount, strHost
'---------------------------------------------------------------------------------------------
strHost = Request.ServerVariables("HTTP_HOST")
'-- REMEMBER TO EDIT THE FIRST CASE STATEMENT, OF THE CODE WILL NOT WORK
'-- I ADD A CASE STATEMENT FOR EACH ENVIRONMENT THE SCRIPT WILL RESIDE SO I DON'T HAVE TO EDIT
'-- THE TWO VARIABLES BELOW WHEN MOVED FROM DEVELOPEMNT TO PRODCUTION.
Select Case strHost
Case "acctdev.int.westgroup.com"
strDirName = Server.MapPath("/global_planning/docs/")
strMyPath = Server.MapPath("/global_planning/docs/")
Case Else
strDirName = Server.MapPath("/gp/docs/")
strMyPath = Server.MapPath("/gp/docs/")
End Select
'---------------------------------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set strFolder = objFSO.GetFolder(strDirName)
Set strFiles = strFolder.Files
intFileCount = strFolder.Files.Count
Dim strPath '-- PATH OF DIRECORY TO DISPLAY
Dim objFolder '-- FOLDER VARIABLE
Dim objItem '-- VARIABLE USED TO LOOP THROUGH AND DISPLAY THE CONTENTS
strPath = strMyPath
'-- CREATE THE FILE SYSTEM OBJECT
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'-- SET THE OBJECT AND PASS IN THE PATH AS AN ARGUMENT
Set objFolder = objFSO.GetFolder(strPath)
%>
<table align="center" border="2" bordercolor="#103052" cellspacing="0" cellpadding="2" width="100%">
<%
If intFileCount = "0" Then
%>
<tr>
<td colspan="5" nowrap>Sorry, there are no files located in this directory.</td>
</tr>
</table>
<%
Else
%>
<tr bgcolor="#103052">
<td align="CENTER"><font color="#FFFFFF"><b>File Name:</b></font></td>
<td align="CENTER"><font color="#FFFFFF"><b>Date Created:</b></font></td>
<td align="CENTER"><font color="#FFFFFF"><b>File Type:</b></font></td>
</tr>
<%
'-- FIRST OFF DEAL WITH THE SUBDIRECTORIES
For Each objItem In objFolder.SubFolders
'-- DEAL WITH THE VTI'S THAT GIVE USERS 404 ERRORS
If InStr(1, objItem, "_vti", 1) = 0 Then
If Not objItem.Name = "Index.asp" Then
%>
<tr bgcolor="#eff7ff">
<td align="left" nowrap><a href="<%=objItem.Name %>" target="_TOP"><%=objItem.Name%></a></td>
<td align="left" nowrap><%=objItem.DateCreated%></td>
<td align="left" nowrap><%=objItem.Type%> </td>
</tr>
<%
End If
End If
Next
'-- NEXT OBJITEM IN THE COLLECTION
'-- NOW THAT THE SUBFOLDERS ARE CREATED, CREATE THE FILES
For Each objItem In objFolder.Files
If Not objItem.Name = "Index.asp" Then
'-- CHANGE THE COLOR OF THE ROW IF DISPLAYING A FILE FOLDER
%>
<tr>
<td align="left" nowrap><a href="<%=objItem.Name %>" target="_TOP"><%=objItem.Name%></a></td>
<td align="left" nowrap><%=objItem.DateCreated %></td>
<td align="left" nowrap><%=objItem.Type %></td>
</tr>
<%
End If
Next
'-- NEXT OBJITEM IN THE COLLECTION
End If
'-- KILL THE OBJECT VARIABLES
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
%>
</table>
</body>
</html>