Function CountFiles(Directory As String, NameFragment As String)
Dim fso As Object, fld As Object, sf As Object, fil As Object
Dim Count As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(Directory)
' Remove if you do not want to check the directory itself
For Each fil In fld.Files
If LCase(fil.Name) Like LCase(NameFragment) Then Count = Count + 1
Next
For Each sf In fld.SubFolders
For Each fil In sf.Files
If LCase(fil.Name) Like LCase(NameFragment) Then Count = Count + 1
Next
Next
CountFiles = Count
Set fil = Nothing
Set sf = Nothing
Set fld = Nothing
Set fso = Nothing
End Function
|