VB编程:获取文件夹下所有文件
- Private Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String) As Long
- DoEvents
- Static Files As Long
- Dim sDir As String
- Dim sSubDirs() As String
- Dim Index As Long
- Dim sFiles() As String
-
- If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
- '获取文件名和数目
-
- sDir = Dir(sPath & sFileSpec)
-
- Do While Len(sDir)
-
- Files = Files + 1
- ReDim Preserve sFiles(1 To Files)
- sFiles(Files) = sPath & sDir
-
- '显示到列表
- List1.AddItem sFiles(Files)
- List1.ListIndex = List1.ListCount - 1
-
- sDir = Dir
- Loop
- '获取文件夹名称
- Index = 0
- sDir = Dir(sPath, vbDirectory)
- Do While Len(sDir) 'sDir <> ""
- If sDir <> "." And sDir <> ".." Then
- If GetAttr(sPath & sDir) And vbDirectory Then
- Index = Index + 1
- ReDim Preserve sSubDirs(1 To Index)
- sSubDirs(Index) = sPath & sDir & "\"
- End If
- End If
- sDir = Dir
- Loop
- '递归调用,获取子文件夹目录
- For Index = 1 To Index
- Call TreeSearch(sSubDirs(Index), sFileSpec)
- Next Index
- TreeSearch = Files
- End Function
- Private Sub Command1_Click()
-
- Print TreeSearch(Text1.Text, Combo1.Text)
-
- End Sub
- Private Sub Command2_Click()
- End
- End Sub
-
-
- 'Private Sub Command3_Click()
- ' List1.Clear
- 'End Sub
- Private Sub Form_Load()
- Text1.Text = "D:\vb小程序\vb实例"
- Dim spec As Variant
- spec = Array("*.*", "*.exe", "*.vbp")
- Combo1.Text = spec(0)
- For i = 0 To UBound(spec)
- Combo1.AddItem spec(i)
- Next i
- End Sub