是否可以提供find2perl mindepth和maxdepth参数?

时间:2012-11-14 07:50:14

标签: perl

我没有看到find2perl文档提及有关支持-mindepth-maxdepth参数的任何内容。

以下示例适用于find

$ find2perl . -mindepth 2 -maxdepth 2 -name "*txt" -type f
Unrecognized switch: -mindepth

问题:

  • find2perl是否支持此类功能?
  • 如果是,我该如何指定mindepthmaxdepth

2 个答案:

答案 0 :(得分:4)

File::Find::Rule有这些选项,它有一个命令行程序findrule

如果你想用File :: Find来做,你可以通过检查文件的深度来实现maxdepth,并在你走得太深时设置$File::Find::prune。 mindepth类似,但你早点从你的函数返回。我今天早上感觉很懒,所以我会把编码留给别人。

更新:其他人做了编程,即File :: Find :: Rule。 Here's the code they use

my $maxdepth = 2;
my $mindepth = 2;
my $topdir   = "something/something/something";
sub wanted {
    # figure out the relative path and depth
    my $relpath = $File::Find::name;
    $relpath =~ s{^\Q$topdir\E/?}{};
    my $depth = File::Spec->splitdir($relpath);

    defined $maxdepth && $depth >= $maxdepth
       and $File::Find::prune = 1;

    defined $mindepth && $depth < $mindepth
       and return;

    ...your code goes here...
}

find \&wanted, $topdir;

答案 1 :(得分:1)

我只是查看了find2perl的源代码,以及File :: Find,它是find2perl使用的目录遍历器。目前尚未实施mindepth和maxdepth。

File :: Find在名为$ CdLvl的变量中遍历目录时显示轨道深度。您可以通过与$ CdLvl进行比较来实现mindepth和maxdepth。

相关问题