VB icon

Display Directory Listing

Email
Submitted on: 1/5/2015 9:00:00 PM
By: Matt Khoury (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: ASP (Active Server Pages)
Views: 840
 
     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!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Display Directory Listing
' Description: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!
' By: Matt Khoury (from psc cd)
'
' Inputs:Edit the SELECT CASE Statement to fit YOUR needs. YOU MUST EDIT THIS!
'
' Returns:Returns the contents of a directory.
'
' Assumes: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.
'
' Side Effects:None.
'**************************************

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


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.