Public Function GetAllFiles(ByVal sPath As String, list As Collection)
Dim item As String
Dim oPaths As New Collection
item = Dir(sPath, vbDirectory)
While Len(item) >0
If item <>"." And item <>".." Then
If (GetAttr(FullPathName(sPath) &item) And vbDirectory) = vbDirectory Then
oPaths.Add item
Else
If IsModelFile(item) Then list.Add sPath &item
End If
End If
item = Dir
Wend
Dim p
For Each p In oPaths
Call GetAllFiles(Combin(sPath, p), list)
Next
End Function
窗体上有四个控件,命令按钮command1。列表框list1。驱动器列表Drive1。文件夹列表Dir1。驱动器和文件夹列表是用来选择你想要搜索的文件夹。
程序运行时选中你想要搜索的文件夹,单击搜索按钮。该文件夹下面所有的文件及子文件夹里面的文件都列到列表框中。
将代码复制到窗体即可。代码如下:
Private Sub Command1_Click()
List1.Clear
sosuofile (Dir1.List(Dir1.ListIndex))
MsgBox "搜索完毕!,共找到" + Str(List1.ListCount) + "条记录。", vbOKOnly + vbExclamation, "提示"
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Sub sosuofile(MyPath As String)
Dim Myname As String
Dim dir_i() As String
Dim i, idir As Long
If Right(MyPath, 1) <>"\\" Then MyPath = MyPath + "\\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While Myname <>""
If Myname <>"." And Myname <>".." Then
If (GetAttr(MyPath &Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = Myname
Else: List1.AddItem "" &MyPath &" " &Myname '把找到的文件显示到列表框中
End If
End If
Myname = Dir '搜索下一项
Loop
For i = 0 To idir - 1
Call sosuofile(MyPath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub
Private Sub Form_Load()
Command1.Caption = "搜索"
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)