以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]递归-文件查找报错 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=141609) |
-- 作者:天一生水 -- 发布时间:2019/10/6 20:32:00 -- [求助]递归-文件查找报错 老师好! 当查找路径为根目录时(测试实例设置为d:\\),报错如图;但关闭报错窗口后,会继续完成查找;
另外,查找范围不是根目录时,不会报错。 请老师看看是自定义函数-“递归”哪里的问题。 代码如下: 自定义函数-递归 Dim path As String = args(0) Dim ls = 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) Functions.Execute("递归", p, ls) Next --------------- 按钮-查找 \'\'\' e.Form.Controls("TextBox1").text = "" \'先清空 e.Form.Controls("TextBox1").text = "文件查找中......" Dim count As Integer = 0 \'设置计数器 Dim str As String = "" Dim path As String = e.Form.Controls("TextBox2").text \'文件夹路径 Dim s1 As String = e.Form.Controls("TextBox3").text \'文件名关键字 If path = "" Then msgbox("请获取文件夹路径!") Else If s1 <> "" Then Dim stt As Date = Date.Now \'开始计时 Dim lst1 As new List(Of String) Functions.Execute("递归" ,path , lst1) For Each nm As String In lst1 If s1 <> "" AndAlso nm.Contains(s1) Then str &= FileSys.GetName(nm) & vbcrlf count += 1 End If Next e.Form.Controls("TextBox1").text = "文件查找完毕!" & vbcrlf & "共查找到文件" & count & "个," & "耗时:" & (Date.Now - stt).TotalSeconds & "秒," & vbcrlf & vbcrlf & str End If End If |
-- 作者:有点蓝 -- 发布时间:2019/10/6 20:55:00 -- 函数没有问题。有些系统隐藏目录没有权限读取的 |
-- 作者:天一生水 -- 发布时间:2019/10/6 21:31:00 -- 这样为什么不能避免弹出报错窗口? Try Functions.Execute("递归" ,path , lst1) Catch ex As Exception MessageBox.Show("系统文件没有权限读取!","提示",MessageBoxButtons.OK) End Try ...... |
-- 作者:有点蓝 -- 发布时间:2019/10/6 21:43:00 -- 参考:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=118497&skin=0 |
-- 作者:天一生水 -- 发布时间:2019/10/8 21:08:00 -- 蓝老师好! 这个判断怎样加进下面的代码? For Each f As String In FileSys.GetDirectories("d:\\") Dim s As new System.Security.AccessControl.DirectorySecurity(f, System.Security.AccessControl.AccessControlSections.Access) \'output.show(s.AreAccessRulesProtected & " " & f) If s.AreAccessRulesProtected =flash Then next ----------------------------- \'\'\' e.Form.Controls("TextBox1").text = "" \'先清空 e.Form.Controls("TextBox1").text = "文件查找中......" Dim count As Integer = 0 \'设置计数器 Dim str As String = "" Dim path As String = e.Form.Controls("TextBox2").text \'文件夹路径 Dim s1 As String = e.Form.Controls("TextBox3").text \'文件名关键字 If path = "" Then msgbox("请获取文件夹路径!") Else If s1 <> "" Then Dim lst1 As new List(Of String) Functions.Execute("递归" ,path , lst1) For Each nm As String In lst1 If s1 <> "" AndAlso nm.Contains(s1) Then str &= FileSys.GetName(nm) & vbcrlf count += 1 End If Next End If End If |
-- 作者:有点蓝 -- 发布时间:2019/10/8 22:11:00 -- "递归"函数的代码? |
-- 作者:天一生水 -- 发布时间:2019/10/9 20:31:00 -- 老师帮忙改一下吧,不会改这个"递归"自定义函数: Dim path As String = args(0) Dim ls = args(1) For Each file As String In FileSys.GetFiles(path) ls.add(file) 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 |
-- 作者:狐狸爸爸 -- 发布时间:2019/10/9 22:53:00 -- 内部函数,函数名:递归,代码: 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) 测试通过 |
-- 作者:天一生水 -- 发布时间:2019/10/13 15:27:00 -- 蓝老师好! 下面的代码,nm 是全路径文件名,因此查询结果是目录或文件名包含关键字的。 如果只查询包含关键字的目录(及其所有子目录)应该怎样修改加黑代码? 谢谢! \'\'\' e.Form.Controls("TextBox1").text = "" \'先清空 e.Form.Controls("TextBox1").text = "文件查找中......" Dim count As Integer = 0 \'设置计数器 Dim str As String = "" Dim path As String = e.Form.Controls("TextBox2").text \'文件夹路径 Dim s1 As String = e.Form.Controls("TextBox3").text \'文件名关键字 If path = "" Then msgbox("请获取文件夹路径!") Else If s1 <> "" Then Dim lst1 As new List(Of String) Functions.Execute("递归" ,path , lst1) For Each nm As String In lst1 If s1 <> "" AndAlso nm.Contains(s1) Then ’nm 是全路径文件名,我想查询目录及其下面的子目录 str &= FileSys.GetName(nm) & vbcrlf End If Next End If End If |
-- 作者:有点蓝 -- 发布时间:2019/10/13 23:05: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) 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) |