How do I sort a list of files?
One of IIS' great built-in tools is FileSystemObject. Unfortunately, there is no native way to produce a file listing in the order you specify. For example, consider the following code snippet:
This produces a list of files in seemingly random order. It is not ordered by name, file size, type, date modified, date accessed, archive, read-only, system, hidden, super hidden...
Here are three ways you can order your files alphabetically; the first uses a VBScript sort routine, the second uses a JScript sort routine, and the third uses a database.
Here is the VBScript version (thanks for the help with this, Luke Magnus):
Here is the JScript sort version:
And here is the database version (you'll want to create a simple table, called fileTable, with a filename column, varchar(255)):
The bonus with the database version is that you can insert other elements (such as extension or DateLastModified), and order in different ways (file type; creation date, ascending or descending; reverse alphabetical; etc...). A slightly more efficient way to generate a list of files ordered in whatever way you like would be to use a recordset opened as adOpenDynamic:
Related ArticlesCan I include a file in both server-side and client-side script?
Can I read / write a user's file without a prompt?
Can I rename a file using FileSystemObject?
Could I get some help working with files using FileSystemObject?
How do I avoid 'the red x' when an image is missing?
How do I change the modified time of a file?
How do I create / manipulate images from ASP?
How do I dynamically include files?
How do I find the owner, author, and other properties of a file?
How do I get a list of a folder's subfolders?
How do I get the name of the current URL / page?
How do I prevent people from 'leeching' my CSS or JS files?
How do I prevent people from 'leeching' my images?
How do I retrieve a random file from a given folder?
How do I send the correct filename with BinaryWrite?
How do I use FileSystemObject to create a file on the client?
Why do I get 'Disk not ready' errors with FileSystemObject?
Why do I get 'Invalid procedure call or argument'?
Why do I get 'Path not found' errors with Scripting.FileSystemObject?
Why do I get 'Permission Denied' errors with FileSystemObject?
Why do I get 800A0034 errors?
Why do I get 800A003E / Input past end of file errors?
Why do I get 800A0BBA errors?
Why do I get 800A0BBC errors?
Why do I get an 'Invalid Path Character' error?
Why do I get permissions errors after upgrading to Windows XP?
Why does FileSystemObject hang all of a sudden?
Why is 'the operation completed successfully' an error message?