Sort Folder Contents with FSO

Eventually, there comes a time when you need to display the contents of a folder and have it sorted by name, size, type, date created or some other criteria.

The easiest way to display the contents of a folder is to just output each file name with FSO. However, you are not guaranteed to view the files in the the order you may need.

Whenever I run into this situation, I put all the files into a recordset using a few functions and a table to hold the contents. The example below shows all the files in a folder where I threw in various types of files to demonstrate how this works.

Current Sort and Order: Type DESC  • Total Files: 29

  Name File Type Size
1   invoices and other intrigues.xls XLS File   12 k
2   grid info.xls XLS File   12 k
3   budget.xls XLS File   12 k
4   funny sound.wav Wave Sound   58 b
5   upload_mail.txt Notepad++ Document   138 b
6   twomail3.txt Notepad++ Document   210 b
7   twomail2.txt Notepad++ Document   198 b
8   multi3.txt Notepad++ Document   3 k
9   looping.txt Notepad++ Document   3 k
10   ski_powder_01_thumb.jpg JPEG image   1 k
11   ski_jump_01_thumb.jpg JPEG image   591 b
12   nature_snow_01_thumb.jpg JPEG image   740 b
13   colnogo frame 2.jpg JPEG image   65 k
14   colnogo frame 1.jpg JPEG image   68 k
15   board_jump_01_thumb.jpg JPEG image   453 b
16   testform.htm HTML Document   1 k
17   sendaform.htm HTML Document   995 b
18   index.htm HTML Document   6 k
19   senderpdf.eps EPS File   20 k
20   endings.doc DOC File   23 k
21   beginnings.doc DOC File   44 k
22   maillist.csv CSV File   86 b
23   looping.con CON File   40 b
24   fileto.con CON File   236 b
25   my css.css Cascading Style Sheet Document   4 k
26   inc_teams.asp ASP File   250 b
27   inc_schedule.asp ASP File   28 k
28   inc_rules.asp ASP File   12 k
29   inc_accommodations.asp ASP File   5 k

Now, this page actually uses a feature rich version of the kc_fsoFiles function which allows you to re-sort the files based on user input (i.e., when you click on the headers of the table in this example, the files will be resorted) and count the total files as well. I'll leave that for you to figure out on your own.

See 'da Code

<%
'**********
'kc_fsoFiles
'Purpose:
' 1. To create a recordset using the FSO object and ADODB
' 2. Allows you to exclude files from the recordset if needed
'Use:
' 1. Call the function when you're ready to open the recordset
' and output it onto the page.
' example:
' Dim rsFSO, strPath
' strPath = Server.MapPath("\PlayGround\FSO\Stuff\")
' Set rsFSO = kc_fsoFiles(strPath, "_")
' The "_" will exclude all files beginning with 
' an underscore 
'**********
Function kc_fsoFiles(theFolder, Exclude)
Dim rsFSO, objFSO, objFolder, File
  Const adInteger = 3
  Const adDate = 7
  Const adVarChar = 200
  
  'create an ADODB.Recordset and call it rsFSO
  Set rsFSO = Server.CreateObject("ADODB.Recordset")
  
  'Open the FSO object
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  
  'go get the folder to output it's contents
  Set objFolder = objFSO.GetFolder(theFolder)
  
  'Now get rid of the objFSO since we're done with it.
  Set objFSO = Nothing
  
  'create the various rows of the recordset
  With rsFSO.Fields
    .Append "Name", adVarChar, 200
    .Append "Type", adVarChar, 200
    .Append "DateCreated", adDate
    .Append "DateLastAccessed", adDate
    .Append "DateLastModified", adDate
    .Append "Size", adInteger
    .Append "TotalFileCount", adInteger
  End With
  rsFSO.Open()
	
  'Now let's find all the files in the folder
  For Each File In objFolder.Files
	
    'hide any file that begins with the character to exclude
    If (Left(File.Name, 1)) <> Exclude Then 
      rsFSO.AddNew
      rsFSO("Name") = File.Name
      rsFSO("Type") = File.Type
      rsFSO("DateCreated") = File.DateCreated
      rsFSO("DateLastAccessed") = File.DateLastAccessed
      rsFSO("DateLastModified") = File.DateLastModified
      rsFSO("Size") = File.Size
      rsFSO.Update
    End If

  Next
	
  'And finally, let's declare how we want the files 
  'sorted on the page. In this example, we are sorting 
  'by File Type in descending order,
  'then by Name in an ascending order.
  rsFSO.Sort = "Type DESC, Size ASC "

  'Now get out of the objFolder since we're done with it.
  Set objFolder = Nothing

  'now make sure we are at the beginning of the recordset
  'not necessarily needed, but let's do it just to be sure.
  rsFSO.MoveFirst()
  Set kc_fsoFiles = rsFSO
	
End Function

'Now let's call the function and open the recordset on the page
'the folder we will be displaying
Dim strFolder : strFolder = Server.MapPath("\PlayGround\FSO\stuff\")

'the actual recordset we will be creating with the kc_fsoFiles function
Dim rsFSO 'now let's call the function and open the recordset

'we will exclude all files beginning with a "_"
Set rsFSO = kc_fsoFiles(strFolder, "_")

'now we'll create a loop and start displaying the folder
'contents with our recordset. Of course, this is just a
'simple example and not very well formatted, i.e., not in
'a table, but it gets the point across on how you can
'ouput the recordset on the page.
  While Not rsFSO.EOF
%>
    <p><%= rsFSO("Name").Value %> | <%= rsFSO("Type").Value %></p>
<%
  'and let's move to the next record
     rsFso.MoveNext()
   Wend
  
  'finally, close out the recordset
  rsFSO.close()
  Set rsFSO = Nothing
%>
 

All of the code and examples presented in my playground are either modifications of open source code or original works of Kindler Chase DBA Roubaix Interactive or a combination of both and are Copyright protected unless noted otherwise. You may not use the code here without asking first. OK, so I know you're not going to ask, so how about a nice thank you for playing gift?

 •  got css?  •  got xhtml?  •  trump salad?

This page last updated on 5/25/2020