Forums

This topic is locked

Displaying contents of a directory

Posted 14 Nov 2002 21:01:07
1
has voted
14 Nov 2002 21:01:07 Matthew Tonkin posted:
I need to display the contents of a directory on a intranet site. As documents or spreadsheets are added the web page needs to dynamically add the name of the file and create a hyperlink to the file. Is there any easy way to do this?

Replies

Replied 14 Nov 2002 22:22:03
14 Nov 2002 22:22:03 Jeremy Conn replied:
Here's a cool bit of code that will display all the contents of a folder, including subfolders and files. Then, it makes them into a 'tree' structure that allows you to browse and view the contents.

You will have to change two parts of this code that are shown according to their color below:

<font color=red><b>FOLDER LOCATION</b></font id=red>
<font color=green><b>WEBSITE URL</b></font id=green>

___________________________________________________________________


&lt;%
Const MSG_PATH_INVALID = "Invalid Path!" 'Folder Path not found message

Dim strFolderPath 'Absolute Hard Drive Path
Dim intFolCnt 'Folder Count

intFolCnt = 0
strFolderPath = Server.MapPath("<font color=red><b>images</b></font id=red>"

'Recursive Folder Reader - outputs Files and Folders
Function BrowseFolder(strFolderPath)
Dim objFSO, objFolders, objFile
Dim strOutput

strOutput = ""
intFolCnt = intFolCnt + 1

Set objFSO = Server.CreateObject("Scripting.FileSystemObject"

If objFSO.FolderExists(strFolderPath) Then
Set objFolders = objFSO.GetFolder(strFolderPath)

If intFolCnt &lt;&gt; 1 Then
strOutput = strOutput & _
"&lt;DIV class=Folder onclick=""toggleFolder('"&intFolCnt&"')""&gt;" & objFolders.Name & " &lt;I&gt;(" & FileSize(objFolders.Size) & "&lt;/I&gt;&lt;/DIV&gt;" & vbcrlf & _
"&lt;DIV class=FolderCont ID='FC"&intFolCnt&"'&gt;" & vbcrlf
End If

'Loop Through Folder
For Each objSubFolder in objFolders.SubFolders
'Recurse Subdirectories
strOutput = strOutput & BrowseFolder(objSubFolder) & vbcrlf
Next

'Loop Through Files
For Each objFile in objFolders.Files
strOutput = strOutput & "&lt;DIV class=File&gt;&lt;A HREF='<font color=green><b>localhost/test/images</b></font id=green>"&Link(objFile.Path)&"'&gt;" & objFile.Name & "&lt;/A&gt; &lt;I&gt;(" & FileSize(objFile.Size) & "&lt;/I&gt;&lt;/DIV&gt;" & vbcrlf
Next

If intFolCnt &lt;&gt; 1 Then
strOutput = strOutput & "&lt;/DIV&gt;" & vbcrlf
End If
Else
strOutput = MSG_PATH_INVALID '& " - " & strFolderPath
End If

'return
BrowseFolder = strOutput
End Function

Function Link(strFilePath)
strLink = Replace(lcase(strFilePath), lcase(strFolderPath), ""
strLink = Replace(strLink, "\","/"
Link = strLink
End Function

'Returns File size in nice format
Function FileSize(intFileSize)
IF intFileSize &gt; 1000000 Then
FileSize = (int(intFileSize/1024)+ 1)/1000 & " MB"
ElseIf intFileSize &gt; 1000 Then
FileSize = int(intFileSize/1024)+ 1 & " KB"
Else
FileSize = intFileSize & " Bytes"
End If
End Function

%&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Directory List&lt;/TITLE&gt;
&lt;STYLE&gt;
BODY, TD, DIV, P, SPAN {font-family:verdana;font-size:8pt;letter-spacing:-0.5pt;}
.FolderCont{padding-left:16px;display:none;width:100%;}
.Folder{font-weight:bold;cursor:hand;padding:1px;color:#333333;}
.FolderO{font-weight:bold;cursor:hand;padding:1px;color:#777777;}
.File{padding:1px;}
I{color:#777777;font-weight:normal;}
&lt;/STYLE&gt;
&lt;SCRIPT LANGUAGE=javascript&gt;
&lt;!--
function toggleFolder(strID){
var objRef = eval("FC"+strID);
if (objRef.innerHTML !== ""{
if (objRef.style.display == "inline"{
objRef.style.display = "none";
} else {
objRef.style.display = "inline";
}
}
}

function hilite(){
var el = window.event.srcElement;

if (el.className == "Folder"{
el.className = "FolderO";
} else if (el.className == "FolderO"{
el.className = "Folder";
}
}
//--&gt;
&lt;/SCRIPT&gt;
&lt;/HEAD&gt;
&lt;BODY onmouseover="hilite()" onmouseout="hilite()"&gt;
&lt;%=BrowseFolder(strFolderPath)%&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;

(DWMX|IIS5|Access2K|XP|ASP/VB)
*Connman21*
www.conncreativemedia.com
Replied 14 Nov 2002 22:40:03
14 Nov 2002 22:40:03 Matthew Tonkin replied:
Connman21,

I can't thank you enough!!! I will give it a try.

phi
Replied 15 Nov 2002 17:28:56
15 Nov 2002 17:28:56 Jeremy Conn replied:
Enjoy... <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>

(DWMX|IIS5|Access2K|XP|ASP/VB)
*Connman21*
www.conncreativemedia.com
Replied 16 Nov 2006 13:00:44
16 Nov 2006 13:00:44 Prabhua A replied:
Thats great! i tried yr code works fine.i want to add one more feature.
is it possible to add radio button for each branches of the tree and once radio button is selected that particular name of the folder should be displayed?
thanks in advance.



<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
Enjoy... &lt;img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle&gt;

(DWMX|IIS5|Access2K|XP|ASP/VB)
*Connman21*
www.conncreativemedia.com
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Reply to this topic