'提供一个运用递归查找文件的例子,查询目录和类型可以重新设计一下。VB窗口中,添加一个Commandbutton和List控件。
Private Sub Command1_Click()
List1。Clear
ntx = 0
Call GetDirPath("c:\windows\", "*。
com")
End Sub
Private Sub GetDirPath(CurrentPath As String, ByVal SearFile As String)
Dim nI As Integer, nDirectory As Integer, i As Long
Dim sFileName As String, sDirectoryList() As String
'在当前目录下,首先得到第1个文件
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
List1。
AddItem (CurrentPath + sFileName)
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
GetDirPath sDirectoryList(nI) & "\", SearFile
Next nI
End Sub
。
全部