Trier une liste de fichiers par date

Contenu du snippet

 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
 
 
 Private Function ListeFichier(strPath As String, Optional SortOrder As Integer = 0) As String()
     Dim Fich  As String
     Dim Name  As String
     Dim T()   As String
     Dim D()   As Date
     Dim dt    As Date
     Dim X()   As Long
     Dim P     As Long
     Dim G     As Long
     Dim M     As Long
     Dim nb    As Long
 
     If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
     Fich = Dir(strPath & "*.*", vbHidden Or vbSystem)
 
     Do Until Fich = ""
        Name = strPath & Fich
        dt = FileDateTime(Name)
        
        P = 1: G = nb: M = 0
 
        If nb > 0 Then
           While P < G
              M = (P + G) \ 2
              If dt > D(X(M)) Then P = M + 1 Else G = M
              Wend
           If P > M Then
              If dt > D(X(P)) Then P = P + 1
              End If
           Do While P <= nb
              If dt < D(X(P)) Then Exit Do
              If dt > D(X(P)) Then Exit Do
              P = P + 1
              Loop
           End If
 
        nb = nb + 1
        ReDim Preserve T(nb), D(nb), X(nb)
        If P < nb Then
           CopyMemory X(P + 1), X(P), (nb - P) * 4
           End If
        X(P) = nb
        T(nb) = Name
        D(nb) = dt
 
        Fich = Dir()
        Loop
 
     ReDim temp(nb) As String
     If SortOrder Then
        G = nb: M = -1
        Else
        G = 1: M = 1
        End If
     For P = 1 To nb
         temp(P) = T(X(G))
         G = G + M
         Next
     ListeFichier = temp
     Erase T, temp, D, X
 End Function
 
 

Compatibilité : VB6

Disponible dans d'autres langages :

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.