在立即窗口中查看结果,搜索结果包括文件及文件夹,要复制的话,文件夹里的文件会重复复制,不明白你的意思
Sub test()
FindFile "c:\abc\", "*目标*"
End Sub
Sub FindFile(mPath As String, Optional sFile As String = "")
On Error Resume Next
Dim s As String, sDir() As String
Dim i As Long, d As Long
If Right(mPath, 1) <> "\" Then
mPath = mPath & "\"
End If
'查找目录下的文件
s = Dir(mPath & sFile, vbArchive + vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
Do While s <> ""
Debug.Print mPath & s
s = Dir
Loop
'查找目录下的子目录
s = Dir(mPath, vbArchive + vbDirectory + vbHidden + vbNormal + vbReadOnly + vbSystem)
Do While s <> ""
If s <> "." And s <> ".." Then
If (GetAttr(mPath & s) And vbDirectory) = vbDirectory Then
d = d + 1
ReDim Preserve sDir(d)
sDir(d) = mPath & s
End If
End If
s = Dir
Loop
'开始递归
For i = 1 To d
FindFile sDir(i) & "\", sFile
Next
End Sub