如何遍历某文件夹下的所有文件夹(不要求遍历文件),并一一列出?

xiaoyi1979 2003-09-25 11:16:20
如何遍历某文件夹下的所有文件夹(不要求遍历文件),并一一列出?
比如我要遍历c:\test\的所有文件夹(不包括文件),
在combo1中把文件夹一一列出
...全文
126 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxcc 2003-09-25
  • 打赏
  • 举报
回复
Public Sub ShowFloder(strDir As String, objDir As Object)
On Error Resume Next
Dim lngCount As Long
Dim i As Long
Dim strPath As String


With objDir
.Path = strDir
lngCount = .ListCount
For i = 0 To lngCount - 1
.Path = strDir

strPath = .List(i) ''取得当前子目录

Call AddItemToList(strPath, List1) ''添加到ListBox中

Call ShowFloder(strPath, objDir) ''递归寻找
Next
End With
End Sub

''加载数据到ListBox
Private Sub AddItemToList(ByVal strText As String, ByRef objList As Object)
objList.AddItem strText
End Sub

Private Sub Command1_Click()
Call ShowFloder("c:\test", Dir1)
MsgBox List1.ListCount
End Sub
lxcc 2003-09-25
  • 打赏
  • 举报
回复
速度慢!
最好改改初始目录
lxcc 2003-09-25
  • 打赏
  • 举报
回复
Public Sub ShowFloder(strDir As String, objDir As Object)
On Error Resume Next
Dim lngCount As Long
Dim i As Long
Dim strPath As String


With objDir
.Path = strDir
lngCount = .ListCount
For i = 0 To lngCount - 1
.Path = strDir

strPath = .List(i) ''取得当前子目录

Call AddItemToList(strPath, List1) ''添加到ListBox中

Call ShowFloder(strPath, objDir) ''递归寻找
Next
End With
End Sub

''加载数据到ListBox
Private Sub AddItemToList(ByVal strText As String, ByRef objList As Object)
objList.AddItem strText
End Sub

Private Sub Command1_Click()
Call ShowFloder("c:\", Dir1)
MsgBox List1.ListCount
End Sub
wx05 2003-09-25
  • 打赏
  • 举报
回复
用递归调用一下
xiaoyi1979 2003-09-25
  • 打赏
  • 举报
回复
你这个程序只能遍历一层,我要遍历所有层!
包括文件夹的子文件夹
射天狼 2003-09-25
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
'在窗口中添加一个DirListBox控件

Dim i As Long

Dir1.Path = "C:\"
For i = 0 To Dir1.ListCount - 1
MsgBox Dir1.List(i)
Next
End Sub

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧