Persits Software, Inc. Knowledge Base Articles

HOWTO: Displaying file listings in a reverse order

Problem Description

When using the Directory collection of the Upload object to display a list of files and subfolders in a folder, the collection always returns the list sorted in an ascending order. There is no property or option to sort in a descending order.

Solution

The code sample DirectoryListing.asp which is shipped with AspUpload uses the following iteration method:

Set Upload = Server.CreateObject("Persits.Upload")
Set Dir = Upload.Directory( "c:\MyDir\*.*", SORTBY_NAME)

For Each Item in Dir
   Response.Write Item.FileName
Next

This method returns folder items sorted by a specified criterion in an ascending order only. To reverse the sort order, use the following iteration method instead of For-Each:

For i = Dir.Count to 1 Step -1
   Set Item = Dir(i)
   Response.Write Item.FileName
Next

This, however, will list files first and sub-directories second. To keep sub-directories on top while preserving the reverse order, you may use the following code:

For i = Dir.Count to 1 Step -1
   Set Item = Dir(i)
   If Item.IsSubdirectory Then
      Response.Write Item.FileName
   End If
Next

For i = Dir.Count to 1 Step -1
   Set Item = Dir(i)
   If Not Item.IsSubdirectory Then
      Response.Write Item.FileName
   End If
Next