<% '******************************************************* '* 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 * '******************************************************* %> <% Dim strPath ' Path of directory to read Dim objFSO ' FileSystemObject variable Dim objFolder ' Folder variable Dim objFile ' File variable used to loop through the files ' A recordset object variable and some constants from adovbs.inc. Dim rstFiles Const adVarChar = 200 Const adInteger = 3 Const adDate = 7 ' Set the path to our sample folder. strPath = "dir/" ' Create our FSO and get a handle on our folder Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(Server.MapPath(strPath)) ' Im ignoring and subdirectories and just dealing with the files ' in the specified folder. The ADO Recordset object is going to ' have no attached datasource. I'm just creating it in memory ' and using it to make it easier to deal with the file data. Set rstFiles = Server.CreateObject("ADODB.Recordset") rstFiles.Fields.Append "Name", adVarChar, 255 rstFiles.Fields.Append "Size", adInteger rstFiles.Fields.Append "Date", adDate rstFiles.Fields.Append "Type", adVarChar, 255 rstFiles.Open ' Loop through the files adding the information to our Recordset For Each objFile In objFolder.Files rstFiles.AddNew rstFiles.Fields("name").Value = objFile.Name rstFiles.Fields("size").Value = objFile.Size rstFiles.Fields("date").Value = objFile.DateCreated rstFiles.Fields("type").Value = objFile.Type Next 'objFile ' All done! Kill off our File System Object variables. Set objFile = Nothing Set objFolder = Nothing Set objFSO = Nothing ' Now we've got our file data stored in the Recordset and the ' file system is closed. Now we just use the Recordset object ' to do whatever we want with the data. ' I illustrate sorting in our Sorted Directort List sample: ' http://www.asp101.com/samples/dir_list_sort.asp ' so here I'm going to show how you can perform a query ' just like you would against a database and only show files ' that are smaller then 500 bytes. That will rule out the ' .gif and .jpg files and should leave the .asp, .htm, .rtf, ' and .txt files. rstFiles.Filter = "size < 500" %>

Only showing files that are smaller then 500 bytes:

<% Do While Not rstFiles.EOF %> <% rstFiles.MoveNext Loop %>
File Name: File Size (bytes): Date Created:
<%= rstFiles.Fields("name").Value %> <%= rstFiles.Fields("size").Value %> <%= rstFiles.Fields("date").Value %>
<% ' Close our ADO Recordset object rstFiles.Close Set rstFiles = Nothing %>