Results 1 to 2 of 2

Thread: help - Sorting files in a folder

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    2

    Question

    I want to sort a list of files in a folder before reading through them.
    I want to sort by name or by Date/Time Created.
    Any ideas?

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You could just store the list to a Listbox who's Sorted property is set to True, or you could write a simple sort routine yourself, i.e.
    Code:
    Private Sub Command1_Click()
        Dim aFiles() As String
        Dim sDir As String
        Dim nFile As Long
        
        ReDim aFiles(0)
        sDir = Dir("*.*")
        While Len(sDir)
            aFiles(UBound(aFiles)) = sDir
            ReDim Preserve aFiles(UBound(aFiles) + 1)
            sDir = Dir
        Wend
        ReDim Preserve aFiles(UBound(aFiles) - 1)
        
        SortList aFiles
        
        For nFile = 0 To UBound(aFiles)
            'Do whatever with each file here.
            Debug.Print aFiles(nFile)
        Next
        
    End Sub
    
    Private Sub SortList(ByRef aList As Variant)
        Dim sTemp As String
        Dim nIndex As Long
        
        'Simple Sort Routine
        nIndex = 1
        While nIndex < UBound(aList)
            If UCase(aList(nIndex)) < UCase(aList(nIndex - 1)) Then
                sTemp = aList(nIndex - 1)
                aList(nIndex - 1) = aList(nIndex)
                aList(nIndex) = sTemp
                nIndex = nIndex - 1
            Else
                nIndex = nIndex + 1
            End If
        Wend
    End Sub
    [Edited by Aaron Young on 05-22-2000 at 03:14 PM]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width