免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4165 | 回复: 9
打印 上一主题 下一主题

perl 怎么使用Find过滤某些目录 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-07 15:10 |只看该作者 |倒序浏览
perl 可以使用File::Find 模块的find函数遍历目录,但是我想过滤掉某个目录不知道应该怎么做?
希望大虾帮忙 谢了

论坛徽章:
0
2 [报告]
发表于 2011-02-07 19:14 |只看该作者
preprocess
The value should be a code reference. This code reference is used to preprocess the current directory. The name of the currently processed directory is in $File::Find::dir. Your preprocessing function is called after readdir(), but before the loop that calls the wanted() function. It is called with a list of strings (actually file/directory names) and is expected to return a list of strings. The code can be used to sort the file/directory names alphabetically, numerically, or to filter out directory entries based on their name alone. When follow or follow_fast are in effect, preprocess is a no-op.红色部分好像说可以过滤目录。不知道具体怎么做 望高手指教下。。。

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
3 [报告]
发表于 2011-02-08 00:33 |只看该作者
关键就是这几个变量啊
  1. $File::Find::dir is the current directory name,
  2. $_ is the current filename within that directory
  3. $File::Find::name is the complete pathname to the file.
复制代码

论坛徽章:
0
4 [报告]
发表于 2011-02-08 08:58 |只看该作者
回复 3# zhlong8


    怎么让find不进入某个目录,即其他目录照长遍历,而忽略某个子目录。。。?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
5 [报告]
发表于 2011-02-08 14:04 |只看该作者
回复  zhlong8


    怎么让find不进入某个目录,即其他目录照长遍历,而忽略某个子目录。。。?
zzy7186 发表于 2011-02-08 08:58



    用 RE 判断 $File::Find::dir ?

论坛徽章:
0
6 [报告]
发表于 2011-02-08 19:49 |只看该作者
回复 5# zhlong8


    用RE是可以实现但是这个要除去的目录中有多少文件就会调用几次wanted,
    sub wanted{
                  print $File::Find::name unless $File::Find::dir =~ m{/root/test};
               }
    像上面的如果/root/test 中有n个文件print 语句后面的unless 就要执行n+1次。
    不知道有没有方法让unless只执行一次。。。。?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
7 [报告]
发表于 2011-02-08 22:36 |只看该作者
perldoc File::Find | less -p preprocess
"preprocess"
   The value should be a code reference. This code reference is used to
   preprocess the current directory. The name of the currently processed
   directory is in $File::Find::dir. Your preprocessing function is
   called after "readdir()", but before the loop that calls the
   "wanted()" function. It is called with a list of strings (actually
   file/directory names) and is expected to return a list of strings.
   The code can be used to sort the file/directory names alphabetically,
   numerically, or to filter out directory entries based on their name
   alone.
When *follow* or *follow_fast* are in effect, "preprocess" is
   a no-op.

论坛徽章:
0
8 [报告]
发表于 2011-02-08 22:47 |只看该作者
回复 7# flw [ /b]


    我试了,但是没有效果 感觉是自己对这段话理解错了,望版主帮忙。。。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2011-02-08 23:21 |只看该作者
回复  flw [ /b]


    我试了,但是没有效果 感觉是自己对这段话理解错了,望版主帮忙。。。
zzy7186 发表于 2011-02-08 22:47


preprocess => sub { $File::Find::dir eq '/root' ? grep { $_ ne 'test' } @_ : @_ },

大概意思就是说,preprocess 接受一个文件/目录清单[1](刚刚由 readdir 得到),然后需要返回一个待遍历的文件/目录清单。
所以在这里可以控制遍历的顺序(通过 sort),或者排除一些目录或者文件。

[1]:注意这个清单不包括路径,当前路径在 $File::Find::dir 变量中。

论坛徽章:
0
10 [报告]
发表于 2011-02-08 23:57 |只看该作者
回复 9# flw


  多谢版主!
  thank you very much!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP