Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enhanced list folder contents

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi guys, how would i go about dynamically listing the contents of a folder and create links to the listings as well. For instance, i'd like to be able to list all the files with the .doc extension, and also when the list is displayed, i'd like to be able to click on any of the listings and have that file open. thanks in advance.
 
OK, you'll need a file system onbject. What you will want to do is use this object to get the contents of a folder, then retrieves all the files from that folder into a collection.
Create a string variable that holds all the extensions you want to show as links with spaces between them.
Now loop through the file collection, check if right(filename,4) is in the extension string (using InStr), if it is write it out as a link, if it isn't write it out as plain text (or ignore it if you only want the linkable files).

Now to make this a little fancier you could output these each intheir own row and display the file.LastModified and file.Size to get a more "file listing" feel.

I wrote something similar at somepoint to list all of the web documents in a fodler, grouped by extension, with a preview button and an iframe for the pages. If you want more details let me know and I'll try to fig that file off my lap top and post it (after I get some coffee).
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Thanks, yes please try to dig that file up, i'd like to take a look at it. Thanks
 
One major differance here is that instead of using links I used preview buttons because I wanted to create the preview window. I was planning on adding a folder list and making it navigateable as well, just never got to it.
Code:
<%
Option Explicit

Dim fso, folders, files
Dim pathname
If Request.QueryString(&quot;filepath&quot;) <> &quot;&quot; Then
	pathname = Request.QueryString(&quot;filepath&quot;)
Else
	pathname = &quot;/&quot;
End If

Set fso  = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set folders = fso.getFolder(Server.Mappath(pathname))
Set files = folders.files

Dim previewable
previewable = &quot;asp htm html xml xsl jpg gif txt csv&quot;

Dim file, types, fn
For each file in files
	fn = right(file.name,len(file.name) - InStr(file.name,&quot;.&quot;))
	If InStr(types,fn) < 1 Then
		types = types & fn & &quot;#&quot;
	End If
Next

Dim filetypes
fileTypes = split(types,&quot;#&quot;)

Dim content
content = split(types,&quot;#&quot;)

Dim i
For i = 0 to UBound(content) - 1
	content(i) = &quot;<tr class='rowHeader'><td colspan='4' align='center'>&quot;&ucase(fileTypes(i))&&quot; Files</td></tr>&quot;
Next

dim ind,sz
For each file in files
	ind = getIndex(file.name)
	If file.size > 1024 Then 
		sz = Round(file.size/1024,2) & &quot; KB&quot;
	Else
		sz = file.size & &quot; bytes&quot;
	End If

	content(ind) = content(ind) & &quot;<tr><td>&quot;
	If InStr(previewable,fileTypes(ind)) Then
		content(ind) = content(ind) & &quot;<input type=&quot;&quot;button&quot;&quot; onClick=&quot;&quot;document.getElementById('fraPreview').src='&quot;&pathname&file.name&&quot;'&quot;&quot; value=&quot;&quot;Preview&quot;&quot; class=&quot;&quot;btnPreview&quot;&quot;>&quot;
	End If
	content(ind) = content(ind) & &quot;</td><td>&quot; & file.name & &quot;</td><td align='right'>&quot; & sz & &quot;</td><td align='right'>&quot; & file.dateLastModified & &quot;</td></tr>&quot;
Next

Function getIndex(filename)
	Dim en, i
	en = right(file.name,len(file.name) - InStr(file.name,&quot;.&quot;))
	For i = 0 to UBound(fileTypes) - 1
		If lcase(en) = lcase(fileTypes(i)) Then getIndex = i
	Next
End Function

	%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<title>Previewing Files</title>
<style>
	table{
		background-color:#999999;
	}
	table tr{
		background-color:#eeeeee;
	}
	.rowHeader{
		background-color:#cccccc;
	}
	iframe{
		border:2px #999999 solid;
	}
</style>
<style media=&quot;print&quot;>
	iframe{
		display:none;
	}
	.btnPreview{
		display:none;
	}
</style>
</head>
<body>
<table width=&quot;90%&quot;>
	<tr>
		<td colspan=&quot;6&quot; align=&quot;center&quot;>
			Directories
		</td>
	</tr>
	<%
'	Dim f, c
'	c = 0
'	For Each f In folders.getFolders
'		If c mod 6 = 0 Then Response.Write &quot;<tr>&quot;
'		Response.Write &quot;<a href=&quot;&quot;xmlDirListing.asp?pathname=&quot;&pathname&&quot;/&quot;&f.name&&quot;&quot;&quot;>&quot;&f.name&&quot;</a>&quot;
'		If c mod 6 = 5 Then Response.Write &quot;</tr>&quot;
'		c = c+1
'	Next
	%>
</table>
<table width=&quot;90%&quot;>
	<tr>
		<td>
			Preview
		</td>
		<td>
			Filename
		</td>
		<td>
			Size
		</td>
		<td>
			Last Modified
		</td>
		<td valign=&quot;top&quot; rowspan=&quot;<%=UBound(fileTypes)+files.count+1%>&quot;>
			Preview Window<br>
			<iframe id=&quot;fraPreview&quot; height=&quot;100%&quot; width=&quot;100%&quot;></iframe>
		</td>
	</tr>
<%
Dim section
For each section in content
	Response.Write section
Next
%>
</table>
</body>
</html>

I would warn you not to use this exactly as typed because some of it may be to specific for your uses, but it should server as a good example and may help you write yours more easily.
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top