Perl File :: Find :: Rule excluding directories from the array

I have a set of directories in an array like

chdir /tmp;
my @dir=("test" "abc" "def")

I am looking for a way using File :: Find :: Rule to find all files in / tmp recursively , but excluding files from them in @dir.

I could get all the files from @dir

my $rule =  File::Find::Rule->new;
$rule->file;
my @files = $rule->in( @dir );

But I can not deny the condition, that is, exclude @dir. I've tried

chdir /tmp;
@files = File::Find::Rule->new
    ->file()
    ->name(@dir)
    ->prune
    ->in( "." );

but no conclusion.

+4
source share
1 answer

The documentation shows this example:

ignore CVS directories

my $rule = File::Find::Rule->new; $rule->or($rule->new
               ->directory
               ->name('CVS')
               ->prune
               ->discard,
          $rule->new);

Note the use of the null rule. The null rules match everything they see, so the effect is to map (and drop) directories called "CVS" or match something

:

my @exclude_dirs = qw(test abc def);
my $rule = File::Find::Rule->new; 
$rule->or($rule->new
               ->directory
               ->name(@exclude_dirs)
               ->prune
               ->discard,
          $rule->new);
my @files = $rule->in('/tmp');

:

foo@bar:~/temp/filefind> tree
.
├── bar
│   ├── bar.txt
│   └── foobar.txt
├── baz.txt
└── foo
    └── foo.txt

2 directories, 4 files

:

#!/usr/bin/perl
use strictures;
use File::Find::Rule;
use Data::Dump;

my @exclude_dirs = qw(foo);
my $rule = File::Find::Rule->new; 
$rule->or($rule->new
               ->directory
               ->name(@exclude_dirs)
               ->prune
               ->discard,
          $rule->new);
my @files = $rule->in('filefind');
dd \@files;

:

foo@bar:~/temp> perl file-find.pl
[
  "filefind",
  "filefind/baz.txt",
  "filefind/bar",
  "filefind/bar/bar.txt",
  "filefind/bar/foobar.txt",
]
+7

Source: https://habr.com/ru/post/1524279/


All Articles