Option
Explicit
Private
Enum
myStatus
Found = 1
notExists = 2
Error
= 3
End
Enum
Public
Function
SearchFiles(
ByVal
sPath
As
String
, searchFilename
As
String
)
As
String
()
Dim
oFSO
As
Object
Dim
oFolder
As
Object
Dim
oSubFolder
As
Object
Dim
oFile
As
Object
Dim
Out()
As
String
Dim
iPos
As
Integer
Dim
myResult
As
myStatus
iPos = -1
Set
oFSO = CreateObject(
"Scripting.FileSystemObject"
)
Set
oFolder = oFSO.GetFolder(sPath)
For
Each
oSubFolder
In
oFolder.subfolders
For
Each
oFile
In
oSubFolder.Files
myResult = match(oFile.Name, searchFilename)
If
myResult = myStatus.Found
Then
iPos = iPos + 1
ReDim
Preserve
Out(iPos)
Out(iPos) = oSubFolder.Path & "\" & oFile.Name
ElseIf
myResult = myStatus.
Error
Then
Exit
For
End
If
Next
oFile
If
myResult = myStatus.
Error
Then
Exit
For
Next
oSubFolder
Set
oFSO =
Nothing
Set
oFile =
Nothing
Set
oFolder =
Nothing
Set
oSubFolder =
Nothing
SearchFiles = Out
End
Function
Private
Function
match(
ByRef
Text
As
String
,
ByRef
Search
As
String
)
As
myStatus
On
Error
GoTo
Err_Handler
Dim
myRegEx
As
Object
Set
myRegEx = CreateObject(
"VBScript.RegExp"
)
With
myRegEx
.IgnoreCase =
True
.Global =
True
.Pattern = Search
If
myRegEx.Test(Text)
Then
match = myStatus.Found
Else
match = myStatus.notExists
End
If
End
With
Err_Exit:
Exit
Function
Err_Handler:
Err.Clear
match = myStatus.
Error
Resume
Err_Exit
End
Function