以文本方式查看主题 - Foxtable(狐表) (http://www.foxtable.com/bbs/index.asp) -- 专家坐堂 (http://www.foxtable.com/bbs/list.asp?boardid=2) ---- [求助]遍历文件夹下级的所有文件? (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=161508) |
||||
-- 作者:2425004926 -- 发布时间:2021/3/19 16:55:00 -- [求助]遍历文件夹下级的所有文件? 下面是遍历下一级的文件,显示出两级,还报错了,不知错在那里?如何遍历出下下下...级的所有文件,总不能写出n个For Each吧?有什么更好的思路? Dim f As String = "f:\\"
For Each File As String In FileSys.GetFiles(f) Dim a As String = file Output.Show(a) Next For Each dir As String In FileSys.GetDirectories(f) If dir IsNot Nothing Then For Each File2 As String In FileSys.GetFiles(dir) Dim a2 As String = file2 Output.Show(a2) Next End If Next |
||||
-- 作者:有点蓝 -- 发布时间:2021/3/19 17:02:00 -- 递归处理,参考:http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=117734 |
||||
-- 作者:有点蓝 -- 发布时间:2021/3/19 17:05:00 -- 看看:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=141609&skin=0 |
||||
-- 作者:2425004926 -- 发布时间:2021/3/19 17:14:00 -- 非常谢谢! |
||||
-- 作者:2425004926 -- 发布时间:2021/3/25 11:38:00 --
下面的代码我也看了很多遍,可是增加递归函数时s参数总出错,不知道哪里的问题,想用窗口“提取所有文件名称”按钮提取,请老师帮忙看一下,怎么完善一下?谢谢! 内部函数,函数名:递归,代码:
Dim path As String = args(0) Dim ls As List(of String) = args(1) For Each file As String In FileSys.GetFiles(path) Dim finfo As new FileInfo(file) If finfo.Hidden = False Then ls.add(file) End If Next For Each p As String In FileSys.GetDirectories(path) Dim s As new System.Security.AccessControl.DirectorySecurity(p, System.Security.AccessControl.AccessControlSections.Access) If s.AreAccessRulesProtected = False Then Functions.Execute("递归", p, ls) End If Next 调用测试: Dim lst As new List(of String) Functions.Execute("递归", "C:\\foxtable\\Development",lst) MessageBox.show(lst.Count) 测试通过 内部函数,函数名:递归,代码: Dim path As String = args(0) Dim ls As List(of String) = args(1) Dim s1 As List(of String) = args(2) For Each file As String In FileSys.GetFiles(path) If file.Contains(s1) Then Dim finfo As new FileInfo(file) If finfo.Hidden = False Then ls.add(file) End If End If Next For Each p As String In FileSys.GetDirectories(path) Dim s As new System.Security.AccessControl.DirectorySecurity(p, System.Security.AccessControl.AccessControlSections.Access) If s.AreAccessRulesProtected = False Then Functions.Execute("递归", p, ls) End If Next 调用测试: Dim lst As new List(of String) Dim s1 As String = e.Form.Controls("TextBox3").text Functions.Execute("递归", "C:\\foxtable\\Development",lst,s1) MessageBox.show(lst.Count) |
||||
-- 作者:有点蓝 -- 发布时间:2021/3/25 11:44:00 -- 内部函数,函数名:递归,代码: Dim path As String = args(0) Dim ls As List(of String) = args(1) Dim s1 As List(of String) = args(2) For Each file As String In FileSys.GetFiles(path) If file.Contains(s1) Then Dim finfo As new FileInfo(file) If finfo.Hidden = False Then ls.add(file) End If End If Next For Each p As String In FileSys.GetDirectories(path) Dim s As new System.Security.AccessControl.DirectorySecurity(p, System.Security.AccessControl.AccessControlSections.Access) If s.AreAccessRulesProtected = False Then Functions.Execute("递归", p, ls, s1 ) End If Next |
||||
-- 作者:2425004926 -- 发布时间:2021/3/25 11:51:00 -- 增加递归函数时,If file.Contains(s1) Then这一行还是会出错 |
||||
-- 作者:有点蓝 -- 发布时间:2021/3/25 11:53:00 -- Dim s1 As List(of String) = args(2) 改为 Dim s1 As String = args(2)
|
||||
-- 作者:2425004926 -- 发布时间:2021/3/25 14:14:00 -- 测试成功!谢谢老师! 还有一个问题:如果我提取所有下级的 “文件夹名称” Dim path As String = args(0) Dim ls As List(of String) = args(1) Dim s1 As String = args(2) For Each file As String In FileSys.GetFiles(path) If file.Contains(s1) Then Dim finfo As new FileInfo(file) If finfo.Hidden = False Then ls.add(file) End If End If Next For Each p As String In FileSys.GetDirectories(path) Dim s As new System.Security.AccessControl.DirectorySecurity(p, System.Security.AccessControl.AccessControlSections.Access) If s.AreAccessRulesProtected = False Then Functions.Execute("递归", p, ls, s1 ) End If Next 我把GetFiles改成GetDirectories,是否合理?蓝色部分的代码又该怎么改呢?
|
||||
-- 作者:有点蓝 -- 发布时间:2021/3/25 14:18:00 -- 上面代码不需要做任何改动,蓝色代码就是提取所有下级的 “文件夹名称”的用法,测试有什么问题? |