p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: directory browsing


Message #1 by "kwame sarpong" <scoronc45@h...> on Tue, 1 May 2001 16:16:59
I just started learning asp and came across some code that can be used to 
display the contents of a directory. My questions are:
(1) How do you get this code to loop through so all directories in strPath 
will be displayed by this asp code, and
(2) How do you change the strPath to point to a different drive on a server

Thanks.
 CODE:
 Function ShowImageForType(strName)
	Dim strTemp
  strTemp = strName
If strTemp <> "dir" Then
		strTemp = LCase(Right(strTemp, Len(strTemp) - InStrRev
(strTemp, ".", -1, 1)))
	End If
Select Case strTemp
		Case "asp"
			strTemp = "asp"
		Case "dir"
			strTemp = "dir"
		Case "htm", "html"
			strTemp = "htm"
		Case "gif", "jpg"
			strTemp = "img"
		Case "txt"
			strTemp = "txt"
		Case "doc"   
		    strTemp = "doc"
		Case "pdf"
		    strTemp = "pdf"
		Case "xls"
		    strTemp = "xls"
		Case "ppt"
		    strTemp = "ppt"
		Case "zip"
		    strTemp = "zip"
		Case Else
			strTemp = "misc"
		End Select
strTemp = "<IMG SRC=""./images/dir_" & strTemp & ".gif"" WIDTH=16 
HEIGHT=16 BORDER=0>"
ShowImageForType = strTemp
End Function
%>
<% 
Dim strPath    
Dim objFSO     
Dim objFolder  
Dim objItem    
 
strPath = "./"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath)) %>
<BR>

<TABLE BORDER="5" BORDERCOLOR="blue" CELLSPACING="0" CELLPADDING="2">
	<TR BGCOLOR="#330066">
		<TD><FONT COLOR="#FFFFFF"><B>File Name:</B></FONT></TD>
		<TD><FONT COLOR="#FFFFFF"><B>File Size 
(bytes):</B></FONT></TD>
		<TD><FONT COLOR="#FFFFFF"><B>Date Created:</B></FONT></TD>
		<TD><FONT COLOR="#FFFFFF"><B>File Type:</B></FONT></TD>
	</TR>
<%
For Each objItem In objFolder.SubFolders
	 If InStr(1, objItem, "_vti", 1) = 0 Then
%>
	<TR BGCOLOR="#FFFFFF">
		<TD ALIGN="left" ><%= ShowImageForType("dir") %> <A 
HREF="<%= strPath & objItem.Name %>"><%= objItem.Name %></A></TD>
		<TD ALIGN="right"><%= objItem.Size %></TD>
		<TD ALIGN="left" ><%= objItem.DateCreated %></TD>
		<TD ALIGN="left" ><%= objItem.Type %></TD>
	</TR>
	<%
	End If
Next 'objItem

For Each objItem In objFolder.Files
%>
	<TR BGCOLOR="#FFFFFF">
		<TD ALIGN="left" ><%= ShowImageForType(objItem.Name) %
> <A HREF="<%= strPath & objItem.Name %>"><%= objItem.Name %></A></TD>
		<TD ALIGN="right"><%= objItem.Size %></TD>
		<TD ALIGN="left" ><%= objItem.DateCreated %></TD>
		<TD ALIGN="left" ><%= objItem.Type %></TD>
	</TR>
<%
Next 'objItem

Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
%>
</TABLE>



  Return to Index