Sort a folder list retreived from FileSystemObject in reverse file date order.

Posted on 2005-04-18
Last Modified: 2011-10-03
Here is a snip of my current code.  Currently, the folder list is sorted alphabetacly.
I want objFolderContents to be sorted in reverse file date order(newest folder at the top)

Set objFSO = CreateObject("Scripting.FileSystemObject")
strPathInfo = baseDir & "\users\" & gp
Set objFolder = objFSO.GetFolder(strPathInfo)
Set objFolderContents = objFolder.SubFolders
'Run a loop through the directory list
For each FolderItem In objFolderContents

I have found several examples of sorting in ASP but I'm having a lot of trouble getting them to work.  Is it because I am using a "set object"?

Or maybe is it possible to ask FileSystemObject for the list sorted the way I want?

I need an example that will work with my code.

Question by:stanstan123
    LVL 8

    Accepted Solution


    For each FolderItem In objFolderContents


    Dim i
    Dim aoFolders

    i = objFolder.SubFolders.count -1
    ReDim aoFolders(i)

    For each FolderItem in objFolder.SubFolders
          Set aoFolders(i) = FolderItem
          i = i - 1

    For Each FolderItem In aoFolders
    'The rest of you code here.

    aoFolders is array of folder objects held in reverse order.


    LVL 7

    Assisted Solution

    The short answer is that you can't sort the files using the FSO - damn shame!

    The following links give examples of placing the files in an array and then sorting that.

    You can also use javascript to sort them, which would allow the user to choose whether they are sorted by date, size or alphabetically.

    Finally of course, if you only have a few files which never change, you could name them 01file.txt, 02file.txt etc in the order you want.

    LVL 9

    Assisted Solution

    For total flexibility, you should place them inside a Recordset object, and then use the .sort method that is part of that object:

    Dim objFilesRS

    Set objFilesRS = Server.CreateObject("ADODB.Recordset")

    objFilesRS.Fields.Append "FileName", adVarchar, 255
    objFilesRS.Fields.Append "dateCreated" adDate

    For Each file in objFSO

        objFilesRS.Fields("FileName").Value = file.Name
        objFilesRS.Fields("dateCreated").Value = file.dateCreated

    objFilesRS.Sort = "dateCreated DESC"

    This is just a quick example, the following is much more thorough:

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now