In the past two tips, you've been creating a recordset without the use of a database connection. In this tip, you'll learn what you can do with this recordset once it's created. Note that the code shown here, especially the Sort property, works fine regardless of how the recordset is created. The new code is marked in boldface:
<!-- METADATA TYPE="typelib" FILE="C:\Program files\Common files\System\ado\msado15.dll" --!>
<%
Option Explicit
Dim objFSO ' As Scripting.FileSystemObject
Dim objFolder ' As Scripting.Folder
Dim objFile ' As Scripting.File
Dim rsData ' As ADODB.Recordset
Set rsData = Server.CreateObject("ADODB.Recordset")
With rsData.Fields
.Append "Filename", adVarChar, 40, adFldUpdatable
.Append "Size", adInteger, , adFldUpdatable
End With
rsData.Open , , adOpenDynamic
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))
For Each objFile in objFolder.Files
rsData.AddNew
rsData("Filename") = objFile.Name
rsData("Size") = objFile.Size
rsData.Update
Next ' objFile
rsData.Sort = "Filename"
rsData.MoveFirst
Do Until rsData.EOF
Response.Write rsData("Filename") _
& " (" & rsData("Size") & " bytes)
"
rsData.MoveNext
Loop
rsData.Close
%>
By setting the Sort property to one of the fields of data, we automatically sort the list of files before we print them out. If, for instance, you wanted to sort the files by size instead of by name, you would use this line instead:
rsData.Sort = "Size"