Function APIFilterFileListByName(Path As String, Optional SearchString = "*", Optional extension = "*.*", Optional Recursif As Boolean = False, Optional TbL As Variant)
Dim FindData As WIN32_FIND_DATA, FileName$, FullPath$, Debut&, X&, Att
#If VBA7 Then
Dim hFind As LongPtr
#Else
Dim hFind As Long
#End If
On Error Resume Next
'si tbl n'est pas un array c'est que c'est le debut alors on le redim en tableau a zero
If Not IsArray(TbL) Then ReDim TbL(0): Debut = 1
If SearchString = "" Then SearchString = "*"
' Ajouter le separateur si il est manquant
If Right(Path, 1) <> "\" Then Path = Path & "\"
' Ajout de l'argument All (*.*) pour la recherche
Path = Path & "*.*"
' Démarrer la recherche
hFind = FindFirstFile(Path, FindData)
If hFind <> -1 Then
Do
' Extraire le nom du fichier
FileName = Left(FindData.cFileName, InStr(FindData.cFileName, vbNullChar) - 1)
' Ignorer les dossiers "." et ".."
If FileName <> "." And FileName <> ".." And Not FileName Like "*$*" Then
'Concat du fullpath
FullPath = Left(Path, Len(Path) - 4) & "\" & FileName ' concatainer le chemin complet
'Vérifier si ce n'est pas un dossier
'addition logique de l'attribut + vbdirectory (permet d'exclure les dossiers les fichiers system)
Att = (FindData.GetAttribute And vbDirectory)
If Att <> vbDirectory Then
' Vérifier si le nom du fichier contient la chaîne recherchée
If " " & LCase(FileName) Like LCase("*" & SearchString & "*" & extension & "*") Then
'ajouter au tableau si c'est un fichier
X = UBound(TbL) + 1: ReDim Preserve TbL(1 To X): TbL(X) = FullPath
End If
Else
' Appel récursif si c'est un dossier
If Recursif Then APIFilterFileListByName FullPath & "\", SearchString, extension, Recursif, TbL
End If
End If
Loop While FindNextFile(hFind, FindData)
'Fermer le handle de recherche
FindClose hFind
End If
' Return
If Debut = 1 Then APIFilterFileListByName = TbL
On Error GoTo 0
End Function