ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

ASP Source Code:
' Declare our variables
Dim strPath   ' Path of directory to show
Dim objFSO    ' FileSystemObject variable
Dim objFolder ' Folder variable
Dim objFile   ' Variable used to loop through the files
' A recordset object, a looping var and some selected
' constants from  I use these for the sorting code.
Dim rstFiles, objField
Const adVarChar = 200
Const adInteger = 3
Const adDate = 7
' The sorting information we'll retrieve from the QueryString
Dim strSortField ' Field to sort by
Dim strSortOrder ' "ASC" or "DESC"
' You could just as easily read this from some sort of input, but I don't
' need you guys and gals roaming around our server so I've hard coded it to
' a directory I set up to illustrate the sample.
' NOTE: As currently implemented, this needs to end with the /
strPath = "dir/"
' Create our FSO which we'll use to read our file info
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
' Get a handle on our folder
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
' In order to be able to sort them easily and still close the FSO relatively
' quickly I'm going to make use of an ADO Recordset object with no attached
' datasource.  While it does have a slightly greater overhead then an array
' or dictionary object, it gives me named access to the fields and has built
' in sorting functionality.
Set rstFiles = Server.CreateObject("ADODB.Recordset")
rstFiles.Fields.Append "File Name", adVarChar, 255
rstFiles.Fields.Append "File Size (bytes)", adInteger
rstFiles.Fields.Append "Date Created", adDate
rstFiles.Fields.Append "File Type", adVarChar, 255
For Each objFile In objFolder.Files
	rstFiles.Fields("File Name").Value = objFile.Name
	rstFiles.Fields("File Size (bytes)").Value = objFile.Size
	rstFiles.Fields("Date Created").Value = objFile.DateCreated
	rstFiles.Fields("File Type").Value = objFile.Type
Next 'objItem
' All done!  Kill off our FileSystemObject and related objects.
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
' Now we can easily sort our data and display it using the recordset.
' Retrieve sorting parameters:
' Get the field name and make sure the input is one of our field names.
strSortField = Request.QueryString("field")
Select Case LCase(strSortField)
	Case "file size (bytes)", "date created", "file type"
		strSortField = strSortField
	Case Else
		strSortField = "File Name"
End Select
' Check for descending o/w we default to ascending
If LCase(Request.QueryString("order")) = "desc" Then
	strSortOrder = "desc"
	strSortOrder = "asc"
End If
' Do the sorting
rstFiles.Sort = "[" & strSortField & "] " & strSortOrder
' Show a little description line and the title row of our table
Contents of <strong><%= strPath %></strong><br />
<br />
<table border="1" cellspacing="0">
' Write titles and include links to sort the table by each field
Response.Write vbTab & "<tr>" & vbCrLf
For Each objField in rstFiles.Fields
	Response.Write vbTab & vbTab & "<td><strong>"
	If objField.Name = strSortField And strSortOrder = "asc" Then
		Response.Write "<a href=""?field=" & Server.URLEncode(objField.Name) & "&order=desc"">" & Server.HTMLEncode(objField.Name) & "</a>"
		Response.Write "<a href=""?field=" & Server.URLEncode(objField.Name) & "&order=asc"">" & Server.HTMLEncode(objField.Name) & "</a>"
	End If
	Response.Write "</strong>"
	If objField.Name = strSortField Then
		If LCase(strSortOrder) = "asc" Then
			Response.Write " <img src=""images/arr_up2.gif"" width=""9"" height=""7"" border=""0"" alt=""Up Arrow""> "
			Response.Write " <img src=""images/arr_dn2.gif"" width=""9"" height=""7"" border=""0"" alt=""Down Arrow""> "
		End If
	End If
	Response.Write "</td>" & vbCrLf
Next 'objField
Response.Write vbTab & "</tr>" & vbCrLf
' Loop through recordset showing the sorted file data
Do While Not rstFiles.EOF
		<td align="left" ><a href="<%= strPath & rstFiles.Fields("File Name").Value %>"><%= rstFiles.Fields("File Name").Value %></a></td>
		<td align="right"><%= rstFiles.Fields("File Size (bytes)").Value %></td>
		<td align="left" ><%= rstFiles.Fields("Date Created").Value %></td>
		<td align="left" ><%= rstFiles.Fields("File Type").Value %></td>
' Close our ADO Recordset object
Set rstFiles = Nothing
'Close the table

Back the the Sample Output

Back to the Sample Index

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers