开发者

Sort FileSystemObject by date

I would like to sort the files using ASP Classic FSO by date.

Do I need t开发者_如何学Goo loop through all the files, put the dates into an array and then list the array by date order or is there a simpler way to do this using FSO alone?


I created a VBScript function SortFiles which:

  1. Copies FileSystemObject's Files collection into a VBScript array
  2. Bubble sort the VBScript array via descending DateLastModified order (caveat: it's a slow sort, you can improve it with alternate algorithms)

Here's my VBScript function SortFiles:

Function SortFiles(files)
  ReDim sorted(files.Count - 1)
  Dim file, i, j
  i = 0
  For Each file in files
    Set sorted(i) = file
    i = i + 1
  Next
  For i = 0 to files.Count - 2
    For j = i + 1 to files.Count - 1
      If sorted(i).DateLastModified < sorted(j).DateLastModified Then
        Dim tmp
        Set tmp = sorted(i)
        Set sorted(i) = sorted(j)
        Set sorted(j) = tmp
     End If
    Next
  Next
  SortFiles = sorted
End Function

Sample use of the SortFiles function:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim files
Set files = fso.GetFolder(SOME_PATH).Files
Dim file
For Each file in SortFiles(files)
  Rem Do something with file ...
Next


I built a function based on the sort included in ADODB.Recordset that returns a dictionary of paths and modified dates because that's what I needed. You could easily pass back the recordset itself, like seen in the source example. Or you could modify what comes back in the dictionary.

I chose to only return the values I needed because it's faster. If you wanted to get back to FSO.File objects from the path, you can with FSO.GetFile but that will seriously slow you down if you're working with a large number of files.

You can also pass in any sort string you want so it doesn't have to be by date.

You need that error handler because .MoveFirst can throw on an empty recordset. This will close gracefully on empty collection.

Public Function SortFilesByProperty(ByVal fileCollection As Scripting.Files, _
        Optional ByVal sortMethod As String = "DateLastModified ASC") _
        As Scripting.Dictionary

    Dim sortedFiles As New Scripting.Dictionary
    Const adVarChar As Long = 202
    Const adDate As Long = 7
    Const adInteger As Long = 3
    
    Dim recordSorter As Object
    Set recordSorter = CreateObject("ADODB.Recordset")
    With recordSorter
        With .Fields
            .Append "Name", adVarChar, 200
            .Append "Path", adVarChar, 200
            .Append "Type", adVarChar, 200
            .Append "DateCreated", adDate
            .Append "DateLastAccessed", adDate
            .Append "DateLastModified", adDate
            .Append "Size", adInteger
            .Append "TotalFileCount", adInteger
        End With
        
        .Open
        
        Dim thisFile As Scripting.File
        For Each thisFile In fileCollection
            .AddNew
            .Fields("Name") = thisFile.Name
            .Fields("Path") = thisFile.Path
            .Fields("Type") = thisFile.Type
            .Fields("DateCreated") = thisFile.DateCreated
            .Fields("DateLastAccessed") = thisFile.DateLastAccessed
            .Fields("DateLastModified") = thisFile.DateLastModified
            .Fields("Size") = thisFile.Size
            .Update
        Next thisFile
        
        .Sort = sortMethod
        
        On Error GoTo closered
        .MoveFirst
        
        Do While Not (.EOF And .BOF)
            sortedFiles.Add .Fields("Path").Value, _
                .Fields("DateLastModified").Value

            .MoveNext
        Loop
        
closered:
        .Close
    End With
    
    Set SortFilesByProperty = sortedFiles
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜