7,789
社区成员
发帖
与我相关
我的任务
分享Option Explicit
Private FoundFile() As String '存放传回值的字串阵列
Private Ntx As Long
Public Function SearchFileInPath(ByVal thePath As String, ByVal theFileName As String, Optional ByVal mStop As Boolean = False) As String()
If Right(thePath, 1) <> "\" Then thePath = thePath & "\"
Call GetFileLoop(thePath, theFileName, mStop)
SearchFileInPath = FoundFile
End Function
Private Function GetFileLoop(CurrentPath As String, ByVal SearFile As String, Optional ByVal mStop As Boolean = False) As String
Dim nI As Integer, nDirectory As Integer, i As Long
Dim sFileName As String, sDirectoryList() As String
' Ntx = 0
On Error Resume Next
sFileName = Dir(CurrentPath, vbHidden Or vbDirectory Or vbReadOnly Or vbSystem)
Do While sFileName <> ""
If UCase(sFileName) Like UCase(SearFile) Then
i = GetAttr(CurrentPath + sFileName)
If (i And vbDirectory) = 0 Then
If mStop = False Then
ReDim Preserve FoundFile(Ntx)
FoundFile(Ntx) = CurrentPath + sFileName
Ntx = Ntx + 1
Else
GetFileLoop = CurrentPath + sFileName
Exit Function
End If
End If
End If
If sFileName <> "." And sFileName <> ".." Then
If GetAttr(CurrentPath & sFileName) _
And vbDirectory Then
nDirectory = nDirectory + 1
ReDim Preserve sDirectoryList(nDirectory)
sDirectoryList(nDirectory) = CurrentPath & sFileName
End If
End If
sFileName = Dir
Loop
For nI = 1 To nDirectory
GetFileLoop = GetFileLoop(sDirectoryList(nI) & "\", SearFile)
If GetFileLoop <> "" And mStop = True Then Exit For
Next nI
End Function
Private Function GetFileLoop(CurrentPath As String, ByVal SearFile As String, Optional ByVal mStop As Boolean = False) As String
...
Do While sFileName <> ""
'If UCase(sFileName) Like UCase(SearFile) Then <- 修改为
If IsMatch(FileName, SearFile) Then
...
End Function
Private Function IsMatch(ByVal FileName As String, ByVal SearchPattern As String) As Boolean
Dim aPatterns() As String
Dim I As Long
aPatterns = Split(SearchPattern, ";") '约定用分号连接多个条件,比如:*.txt;*旅游.*
For I = 0 To UBound(aPatterns)
If FileName Like aPatterns(I) Then
IsMatch = True
Exit Function
End If
Next
IsMatch = False
End Function For nI = 1 To nDirectory
GetFileLoop = GetFileLoop(sDirectoryList(nI) & "\", SearFile)
If GetFileLoop <> "" And mStop = True Then Exit For
Next nI