I have found that the old CMD DOS Dir
command runs much faster than the filesystem object or vba DIR commands.
Accordingly you can try the following:
Note that the arguments for the DIR command will return the files in a date-sorted order, with the newest being at the top; so once you get a single entry that is earlier than today, you can exit the loop.
The files that "pass the test" are stored in a Collection object, for you to do with what you will.
Option Explicit
Public vFileList As Variant
Public Function RetournerFichierModifieCeJour(PathDossier As String)
Dim vaArray As Variant
Dim I As Long
Dim V As Variant
Dim col As Collection
GetDirTree PathDossier
Set col = New Collection
For I = 0 To UBound(vFileList)
V = Left(vFileList(I), 10)
If IsDate(V) Then
If CDate(V) < (Date) Then Exit For
col.Add Split(Mid(vFileList(I), 40), vbCr)(0)
End If
Next I
End Function
Sub GetDirTree(PathDossier As String)
Dim WSH As WshShell, lErrCode As Long
Dim FSO As FileSystemObject, TS As TextStream
Dim sTemp As String
sTemp = Environ("Temp") & "\FileList.txt"
Set WSH = New WshShell
'note /U to enable Unicode output, as some names have char codes > 127 which are altered by redirection
lErrCode = WSH.Run("CMD /U /c dir """ & PathDossier & """ /A-D-S-H /O-D > " & sTemp, xlHidden, True)
If Not lErrCode = 0 Then
MsgBox "Problem Reading Directory" & _
vbLf & "Error Code " & lErrCode
Exit Sub
End If
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile(sTemp, ForReading, False, TristateTrue)
vFileList = Split(TS.ReadAll, vbLf)
TS.Close
FSO.DeleteFile sTemp
Set FSO = Nothing
End Sub
Oh, this routine took just under two (2) seconds to go through 37,000 files of which 104 passed the test.