Foxtable(狐表)用户栏目专家坐堂 → [求助]遍历文件夹下级的所有文件?


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

主题:[求助]遍历文件夹下级的所有文件?

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


加好友 发短信
等级:三尾狐 帖子:712 积分:5553 威望:0 精华:0 注册:2019/11/14 9:36:00
[求助]遍历文件夹下级的所有文件?  发帖心情 Post By:2021/3/19 16:55:00 [只看该作者]

 
图片点击可在新窗口打开查看此主题相关图片如下:1616143515(1).png
图片点击可在新窗口打开查看

下面是遍历下一级的文件,显示出两级,还报错了,不知错在那里?如何遍历出下下下...级的所有文件,总不能写出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

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107720 积分:547933 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/3/19 17:02:00 [只看该作者]

递归处理,参考:http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=117734

 回到顶部
帅哥,在线噢!
有点蓝
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107720 积分:547933 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/3/19 17:05:00 [只看该作者]

看看:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=141609&skin=0

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


加好友 发短信
等级:三尾狐 帖子:712 积分:5553 威望:0 精华:0 注册:2019/11/14 9:36:00
  发帖心情 Post By:2021/3/19 17:14:00 [只看该作者]

 非常谢谢!


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


加好友 发短信
等级:三尾狐 帖子:712 积分:5553 威望:0 精华:0 注册:2019/11/14 9:36:00
  发帖心情 Post By:2021/3/25 11:38:00 [只看该作者]

 
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:提取名称.rar

下面的代码我也看了很多遍,可是增加递归函数时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)


 回到顶部
帅哥,在线噢!
有点蓝
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107720 积分:547933 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By: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
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:712 积分:5553 威望:0 精华:0 注册:2019/11/14 9:36:00
  发帖心情 Post By:2021/3/25 11:51:00 [只看该作者]

 增加递归函数时,If file.Contains(s1) Then这一行还是会出错

 回到顶部
帅哥,在线噢!
有点蓝
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107720 积分:547933 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/3/25 11:53:00 [只看该作者]

Dim s1 As List(of String) = args(2)

改为
Dim s1 As String = args(2)

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


加好友 发短信
等级:三尾狐 帖子:712 积分:5553 威望:0 精华:0 注册:2019/11/14 9:36:00
  发帖心情 Post By: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,是否合理?蓝色部分的代码又该怎么改呢?

 回到顶部
帅哥,在线噢!
有点蓝
  10楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107720 积分:547933 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2021/3/25 14:18:00 [只看该作者]

上面代码不需要做任何改动,蓝色代码就是提取所有下级的 “文件夹名称”的用法,测试有什么问题?

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