<% '******************************************************* '* ASP 101 Sample Code - http://www.asp101.com/ * '* * '* This code is made available as a service to our * '* visitors and is provided strictly for the * '* purpose of illustration. * '* * '* http://www.asp101.com/samples/license.asp * '* * '* Please direct all inquiries to webmaster@asp101.com * '******************************************************* %> <% ' 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 adovbs.inc. 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 rstFiles.Open For Each objFile In objFolder.Files rstFiles.AddNew 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" Else 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 <%= strPath %>

<% ' Write titles and include links to sort the table by each field Response.Write vbTab & "" & vbCrLf For Each objField in rstFiles.Fields Response.Write vbTab & vbTab & "" & vbCrLf Next 'objField Response.Write vbTab & "" & vbCrLf ' Loop through recordset showing the sorted file data rstFiles.MoveFirst Do While Not rstFiles.EOF %> <% rstFiles.MoveNext Loop ' Close our ADO Recordset object rstFiles.Close Set rstFiles = Nothing 'Close the table %>
" If objField.Name = strSortField And strSortOrder = "asc" Then Response.Write "" & Server.HTMLEncode(objField.Name) & "" Else Response.Write "" & Server.HTMLEncode(objField.Name) & "" End If Response.Write "" If objField.Name = strSortField Then If LCase(strSortOrder) = "asc" Then Response.Write "  " Else Response.Write "  " End If End If Response.Write "
"><%= rstFiles.Fields("File Name").Value %> <%= rstFiles.Fields("File Size (bytes)").Value %> <%= rstFiles.Fields("Date Created").Value %> <%= rstFiles.Fields("File Type").Value %>