开发者

Sorted list of file names in a folder in VBA?

开发者 https://www.devze.com 2022-12-30 02:34 出处:网络
Is there a way to get a sorted list of file names of a folder in VBA? Up to now, I arrived at Dim fso As Object

Is there a way to get a sorted list of file names of a folder in VBA? Up to now, I arrived at

Dim fso As Object
Dim objFolder As Object
Dim objFileList As Object
Dim vFile As Variant
Dim sFolder As String

sFolder 开发者_如何学编程= "C:\Docs"

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(sFolder)
Set objFileList = objFolder.Files

For Each vFile In objFileList
    ' do something '
Next vFile

but it is crucial to be sure the processing order of the for loop is determined by the file names...

Any help appreciated!


Looks like you can do it by using an ADODB.RecordSet. It's a bit heavy duty, but here's a reference which should get you started.


Due to the high number of link only answers, including the accepted one, I will post a tested functional example of quicksort applied to an FSO files collection. The QuickSort will work on any array of strings. I took the code from the link posted by Mitch Wheat in the accepted answer and cleaned it up.

Option Explicit

' Return an array containing the names of the
' files in the directory sorted alphabetically.
Public Function SortedFiles(ByVal dirPath As String) As String()
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim listFolder As Object
    Set listFolder = fso.GetFolder(dirPath)

    ' Make the list of names.
    Dim fileCount As Long
    fileCount = listFolder.Files.Count
    
    Dim fileNames() As String
    ReDim fileNames(1 To fileCount)
    
    Dim i As Long
    i = 1
    
    Dim thisFile As Object
    For Each thisFile In listFolder.Files
        fileNames(i) = thisFile.Name
        i = i + 1
    Next thisFile

    ' Return the sorted list.
    SortedFiles = QuickSort(fileNames, 1, fileCount)
End Function

' Use Quicksort to sort a list of strings.
'
' This code is from the book "Ready-to-Run
' Visual Basic Algorithms" by Rod Stephens.
' http://www.vb-helper.com/vba.htm
Public Function QuickSort(ByVal list As Variant, ByVal min As Long, ByVal max As Long) As String()
                      
    Dim midValue As String
    Dim high As Long
    Dim low As Long
    Dim i As Long
    Dim newList() As String
    newList = list
    
    ' If there is 0 or 1 item in the list,
    ' this sublist is sorted.
    If Not min >= max Then

        ' Pick a dividing value.
        i = Int((max - min + 1) * Rnd + min)
        midValue = newList(i)

        ' Swap the dividing value to the front.
        newList(i) = newList(min)

        low = min
        high = max
        Do
            ' Look down from hi for a value < mid_value.
            Do While newList(high) >= midValue
                high = high - 1
                If high <= low Then Exit Do
            Loop
        
            If high <= low Then
                newList(low) = midValue
                Exit Do
            End If

            ' Swap the lo and hi values.
            newList(low) = newList(high)

            ' Look up from lo for a value >= mid_value.
            low = low + 1
            Do While newList(low) < midValue
                low = low + 1
                If low >= high Then Exit Do
            Loop
        
            If low >= high Then
                low = high
                newList(high) = midValue
                Exit Do
            End If

            ' Swap the lo and hi values.
            newList(high) = newList(low)
        Loop

        ' Sort the two sublists.
        newList = QuickSort(newList, min, low - 1)
        newList = QuickSort(newList, low + 1, max)
    End If
    
    QuickSort = newList
End Function


The order is arbitrary.

When you walk over objFileList add the files to an array, then sort the array.

0

精彩评论

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

关注公众号