vb抓去文件夹目录下所有的文档名称(包括子目录下的文档)
在窗体上添加1个列表框,1个文本框,4个标签,1个按钮。
在文本框里输入你要搜索的某文件夹路径。
代码如下:(已经通过测试)
Option Explicit
Private MyFilesCount As Long
Private Sub Command1_Click()
MyFilesCount = 0
List1.Clear
Label2.Caption = "开始时间:" & Now
Call FindMyPath(Text1.Text)
Label3.Caption = "结束时间:" & Now
Label4.Caption = "搜索到文件数:" & MyFilesCount
End Sub
Public Sub FindMyPath(ByVal MyPath As String)
Dim MyDirs() As String
Dim MyResult As String
Dim MyDirCount As Long
Dim i As Long
MyDirCount = 0
If Right$(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyResult = Dir(MyPath, vbDirectory + vbSystem + vbReadOnly + vbHidden + vbNormal + vbArchive)
Do While Len(MyResult) > 0
DoEvents
If MyResult <> "." And MyResult <> ".." Then
If (GetAttr(MyPath & MyResult) And vbDirectory) <> vbDirectory Then
MyFilesCount = MyFilesCount + 1
List1.AddItem MyPath & MyResult
Else
MyDirCount = MyDirCount + 1
ReDim Preserve MyDirs(MyDirCount)
MyDirs(MyDirCount) = MyPath & MyResult
End If
End If
MyResult = Dir(, vbDirectory + vbSystem + vbReadOnly + vbHidden + vbNormal + vbArchive)
Loop
For i = 1 To MyDirCount
Call FindMyPath(MyDirs(i))
Next i
End Sub
也可以下载附件。