加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

【转】VB问题:如何读取、返回某一目录下的所有文件名

发布时间:2020-12-16 22:50:59 所属栏目:大数据 来源:网络整理
导读:控件:按钮一个 列表框一个 Function SearchFiles(Path As String,FileType As String) Dim Files() As String '文件路径 Dim Folder() As String '文件夹路径 Dim a,b,c As Long Dim sPath As String sPath = Dir(Path FileType) '查找第一个文件 Do While

控件:按钮一个 列表框一个

Function SearchFiles(Path As String,FileType As String)

Dim Files() As String '文件路径

Dim Folder() As String '文件夹路径

Dim a,b,c As Long

Dim sPath As String

sPath = Dir(Path & FileType) '查找第一个文件

Do While Len(sPath) '循环到没有文件为止

a= a + 1

ReDim Preserve Files(1 To a)

Files(a) = Path & sPath '将文件目录和文件名组合,并存放到数组中

List1.AddItem Files(a) '加入list控件中

sPath = Dir '查找下一个文件

DoEvents '让出控制权

Loop

sPath = Dir(Path & "",vbDirectory) '查找第一个文件夹

Do While Len(sPath) '循环到没有文件夹为止

IfLeft(sPath,1) <> "." Then '为了防止重复查找

IfGetAttr(Path & "" & sPath) And vbDirectory Then '如果是文件夹则。。。。。。

b = b + 1

ReDim Preserve Folder(1 To b)

Folder(b) = Path & sPath & "" '将目录和文件夹名称组合形成新的目录,并存放到数组中

End If

End If

sPath = Dir '查找下一个文件夹

DoEvents '让出控制权

Loop

For c = 1 To b '使用递归方法,遍历所有目录

SearchFiles Folder(c),FileType

Next

End Function

Private Sub Command1_Click() '调用

SearchFiles "e:","*.*"

End Sub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读