Foxtable(狐表)用户栏目专家坐堂 → [求助]递归-文件查找报错


  共有2191人关注过本帖树形打印复制链接

主题:[求助]递归-文件查找报错

帅哥哟,离线,有人找我吗?
天一生水
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1137 积分:11224 威望:0 精华:0 注册:2017/9/26 16:30:00
[求助]递归-文件查找报错  发帖心情 Post By:2019/10/6 20:32:00 [只看该作者]

老师好!
当查找路径为根目录时(测试实例设置为d:\),报错如图;但关闭报错窗口后,会继续完成查找;
另外,查找范围不是根目录时,不会报错。
请老师看看是自定义函数-“递归”哪里的问题。


图片点击可在新窗口打开查看此主题相关图片如下:100.jpg
图片点击可在新窗口打开查看
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:递归文件查找(测试).foxdb

代码如下:
自定义函数-递归
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

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106185 积分:540048 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/6 20:55:00 [只看该作者]

函数没有问题。有些系统隐藏目录没有权限读取的

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1137 积分:11224 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/10/6 21:31:00 [只看该作者]

这样为什么不能避免弹出报错窗口?

......
        Try
            Functions.Execute("递归" ,path , lst1)
        Catch ex As Exception
            MessageBox.Show("系统文件没有权限读取!","提示",MessageBoxButtons.OK)
        End Try
......

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106185 积分:540048 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/6 21:43:00 [只看该作者]


 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1137 积分:11224 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By: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

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106185 积分:540048 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/10/8 22:11:00 [只看该作者]

"递归"函数的代码?

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1137 积分:11224 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By: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

 回到顶部
帅哥哟,离线,有人找我吗?
狐狸爸爸
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 帖子:47448 积分:251048 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By: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)

测试通过

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1137 积分:11224 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By: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

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  10楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106185 积分:540048 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By: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)

 回到顶部
总数 12 1 2 下一页