获取文件夹内所有文件(包括子文件夹)

Bullatus 2008-10-06 09:36:14
我要获取指定文件夹下所有文件,及其子文件夹内的所有文件,及子文件夹内子文件夹的所有文件,
以此类推直到最底层。
原来想用递归实现,有没有什么更好更快的方法啊?
...全文
390 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bullatus 2008-10-12
  • 打赏
  • 举报
回复
学到了。
东方之珠 2008-10-08
  • 打赏
  • 举报
回复
用FSO(文件系统对象)最快
xiafan 2008-10-08
  • 打赏
  • 举报
回复
七楼的方法是最有效的。
但也有一个缺点,就是当你的目录选择为根目录时,程序则有可能会出错——如果是 NTFS 分区时,根目录下的“System Volume Information”这个文件夹你是不能简单地进行操作的。
it_gz_xi 2008-10-08
  • 打赏
  • 举报
回复
学习一下
华芸智森 2008-10-07
  • 打赏
  • 举报
回复
Dim Dirs() As String
Dirs = IO.Directory.GetFiles("E:\MP3", "*.*", IO.SearchOption.AllDirectories)
MsgBox(Dirs.Length)
weeyn 2008-10-07
  • 打赏
  • 举报
回复
好多例子
搜搜吧
msnadair 2008-10-07
  • 打赏
  • 举报
回复
唯递归是途也,没有一个现在的方法的。

递归也很简单很美呀!
ZengHD 2008-10-06
  • 打赏
  • 举报
回复
还是递归

Private Sub GetFiles(ByVal strPath As String)
Dim strFiles() As String = System.IO.Directory.GetFiles(strPath)
For Each s As String In strFiles
ListBox1.Items.Add(s)
Application.DoEvents()
Next

Dim strDirectorys() As String = System.IO.Directory.GetDirectories(strPath)
For Each s As String In strDirectorys
GetFiles(s)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetFiles("F:\Software\Visual Studio 98\Disk1\OS")
End Sub
Bullatus 2008-10-06
  • 打赏
  • 举报
回复
依然递归……
essenza 2008-10-06
  • 打赏
  • 举报
回复
Function GetDirectories(path As String, Optional Attributes As VbFileAttribute, _
Optional IncludePath As Boolean) As Collection
Dim dirname As String
Dim path2 As String

' initialize the result
Set GetDirectories = New Collection

' build the path name with a trailing backslash
path2 = path
If Right$(path2, 1) <> "\" Then path2 = path2 & "\"

' start the search
dirname = Dir$(path2 & "*.*", vbDirectory Or Attributes)

Do While Len(dirname)
If dirname = "." Or dirname = ".." Then
' exclude the "." and ".." entries
ElseIf (GetAttr(path2 & dirname) And vbDirectory) = 0 Then
' ignore regular files
Else
' this is a directory
' include the path if requested
If IncludePath Then dirname = path2 & dirname
GetDirectories.Add dirname, dirname
End If
' get next string
dirname = Dir$
Loop

End Function

essenza 2008-10-06
  • 打赏
  • 举报
回复
Function GetDirectories(path As String, Optional Attributes As VbFileAttribute, _
Optional IncludePath As Boolean) As Collection
Dim dirname As String
Dim path2 As String

' initialize the result
Set GetDirectories = New Collection

' build the path name with a trailing backslash
path2 = path
If Right$(path2, 1) <> "\" Then path2 = path2 & "\"

' start the search
dirname = Dir$(path2 & "*.*", vbDirectory Or Attributes)

Do While Len(dirname)
If dirname = "." Or dirname = ".." Then
' exclude the "." and ".." entries
ElseIf (GetAttr(path2 & dirname) And vbDirectory) = 0 Then
' ignore regular files
Else
' this is a directory
' include the path if requested
If IncludePath Then dirname = path2 & dirname
GetDirectories.Add dirname, dirname
End If
' get next string
dirname = Dir$
Loop

End Function

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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