Private Sub Command1_Click()
Dim ff() As String
Dim fn As Long
Dim i As Long
fn = TreeSearch("c:\aaa", "*.txt", ff())
Debug.Print "找到文件数目为" & fn
For i = 1 To fn
Debug.Print ff(i)
Next
End Sub
Private Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String, sFiles() As String) As Long
Static lngFiles As Long
Dim lngIndex As Long
Dim strDir As String
Dim strSubDirs() As String
If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
End If
strDir = Dir(sPath & sFileSpec)
Do While Len(strDir)
lngFiles = lngFiles + 1
ReDim Preserve sFiles(1 To lngFiles)
sFiles(lngFiles) = sPath & strDir
strDir = Dir
Loop
lngIndex = 0
strDir = Dir(sPath & "*.*", 16)
Do While Len(strDir)
If Left(strDir, 1) <> "." Then
If GetAttr(sPath & strDir) And vbDirectory Then
lngIndex = lngIndex + 1
ReDim Preserve strSubDirs(1 To lngIndex)
strSubDirs(lngIndex) = sPath & strDir & "\"
End If
End If
strDir = Dir
Loop
For lngIndex = 1 To lngIndex
Call TreeSearch(strSubDirs(lngIndex), sFileSpec, sFiles())
Next lngIndex
' 显示 C:\ 目录下的名称。
MyPath = "c:\" ' 指定路径。
MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName <> "." And MyName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 如果它是一个目录,将其名称显示出来。
End If
End If
MyName = Dir ' 查找下一个目录。
Loop