Search Persits Software Knowledge Base

 

Article PS01060659

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

Created: 2/8/2002 5:18:38 AM
Last Modified: 2/9/2002 3:26:13 PM

Copyright © Persits Software, Inc. 1998 - 2018
For technical support, write to support@persits.com.