简体   繁体   English

Perl文件::查找::规则

[英]Perl File::Find::Rule

I am trying to copy just the subfolders' names (non-recursively) into an array using File::Find::Rule . 我试图使用File::Find::Rule将子文件夹的名称(非递归地)复制到一个数组中。 I also want to exclude the directory names which are mentioned in array @exclude_dirs 我也想排除数组@exclude_dirs中提到的目录名称

#!/usr/bin/perl

use strict;
use warnings;
use File::Find::Rule;
use Data::Dumper;

my $basedir      = "C:\/Test";
my @exclude_dirs = qw( dir1_excl dir2_excl );

my @subdirs = File::Find::Rule
    ->directory()
  # ->name(@exclude_dirs)->prune->discard, File::Find::Rule->new 
    ->maxdepth(1)
    ->in( $basedir );

print Dumper(\@subdirs);

Desired Output 期望的输出

$VAR1 = [
          'dir1',
          'dir2',
          'dir3'
        ]

Current Output 电流输出

$VAR1 = [
          'C:/Test',
          'C:/Test/dir1',
          'C:/Test/dir1_excl',
          'C:/Test/dir2',
          'C:/Test/dir2_excl',
          'C:/Test/dir3'
        ]

What you were trying for: 您想要的是:

my @subdirs =
     File::Find::Rule
        ->mindepth(1)
        ->maxdepth(1)
        ->directory
        ->or(
            File::Find::Rule
                ->name(@exclude_dirs)
                ->discard
                ->prune,
            File::Find::Rule
                ->new
          )
        ->in($basedir);

Possible optimization: 可能的优化:

my @subdirs =
     File::Find::Rule
        ->mindepth(1)
        ->maxdepth(1)
        ->or(
            File::Find::Rule
                ->name(@exclude_dirs)
                ->discard
                ->prune,
            File::Find::Rule
                ->directory
          )
        ->in($basedir);

That said, all you need is the following: 就是说,您所需要的是以下内容:

my @subdirs =
    File::Find::Rule
        ->mindepth(1)
        ->maxdepth(1)
        ->not_name(@exclude_dirs)
        ->directory
        ->in($basedir);

All of those return full paths, so you'd need to follow up with 所有这些都返回完整路径,因此您需要跟进

s{^\Q$basedir\E/}{} for @subdirs;

Normally, I'd use FFR instead readdir because using readdir is far longer, far more complex and far more error-prone. 通常,我将使用FFR而不是readdir因为使用readdir的时间更长,更复杂并且更容易出错。 But in this case, it's borderline. 但是在这种情况下,这是临界点。

my @subdirs;
{
   my %exclude_dirs = map { $_ => 1 } '.', '..', @exclude_dirs;

   opendir(my $dh, $basedir)
      or die("Can't read dir \"$basedir\": $!\n");

   while (my $fn = readdir($dh)) {
      next if $exclude_dirs{$fn};

      my $qfn = "$basedir/$fn";
      if (!stat($qfn)) {
         warn("Skipping \$qfn\": Can't stat: $!\n");
         next;
      }

      push @subdirs, $fn if -d _;
   }
}
use strict;
use warnings;

use Data::Dumper;

my $basedir      = "C:/Test";
my @exclude_dirs = qw(. .. dir1_excl dir2_excl);
my $exclude_pat  = join('|', map { quotemeta } @exclude_dirs);

opendir(my $dh, $basedir) or die $!;
my @subdirs = grep { -d "$basedir/$_" && !/^(?:$exclude_pat)\z/i } readdir($dh);
closedir($dh);

print Dumper(\@subdirs);

If the directories to exclude aren't as dynamic as they seem in your question, there's no need to build the regular expression at runtime: 如果要排除的目录不像您在问题中所显示的那样动态,则无需在运行时构建正则表达式:

my @subdirs = grep { -d "$basedir/$_" && !/^(?:\.|\.\.|dir1_excl|dir2_excl)\z/i } readdir($dh);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM