Search Tools Links Login

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!

Original Author: Matt Khoury

Inputs

Edit the SELECT CASE Statement to fit YOUR needs. YOU MUST EDIT THIS!

Assumptions

Here's how I use this file. Create an ASP named, "index.asp", or "default.asp" whichever you use. Then place the newly created file in the location where you wish the contents to be displayed. Now, you might have to edit your NT permissions in order to have at least "READ" permissions.

Returns

Returns the contents of a directory.

Side Affects

None.

Code

<%@ Language=VBScript %>
<% Option Explicit %>



Directory Listing



<%
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)
%>


<%
If intFileCount = "0" Then
%>



Sorry, there are no files located in this directory.

<%
Else
%>

File Name:
Date Created:
File Type:

<%
'-- 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
%>

<%=objItem.Name%>
<%=objItem.DateCreated%>
<%=objItem.Type%>

<%
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
%>

<%=objItem.Name%>
<%=objItem.DateCreated %>
<%=objItem.Type %>

<%
End If
Next
'-- NEXT OBJITEM IN THE COLLECTION
End If
'-- KILL THE OBJECT VARIABLES
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
%>


About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 106 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.