sravs448 has asked for the wisdom of the Perl Monks concerning the following question:

Update: Updated the code to a compilable verison as suggested
I am trying to get the list of latest files in each dir(for each project) under a specific path ($output) , excluding a single dir OLD
How can I frame a rule to achieve this?
use strict; use warnings; use Data::Dump; use File::Find::Rule; my $output = "/abc/def/ghi"; my @exclude_dirs = qw(OLD); my $rule = File::Find::Rule->new; $rule->or($rule->new ->file() ->name(@exclude_dirs) ->prune ->discard, $rule->new); my @files = $rule->in("$output"); dd \@files;
Directory Structure
/abc/def/ghi
  |--- Project1
         |-- 2013
                 |-- foobar_2013_0812_154.txt
         |-- 2014
                 |-- foobar__2014_0912_255.txt
                 |-- foobar__2014_0916_248.txt
|--- Project2
         |-- 2013
                 |-- fbaz_2013_0812_154.txt
         |-- 2014
                 |-- fbaz__2014_0912_255.txt
                 |-- fbaz__2014_0916_248.txt
|--- OLD
         |-- projectX

Current Output:
/abc/def/ghi/Project1/ /abc/def/ghi/Project1/2013 /abc/def/ghi/Project1/2013/file1_project1.txt /abc/def/ghi/Project1/20l4 /abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt /abc/def/ghi/Project1/2014/foobar_2014_0916_248.txt /abc/def/ghi/Project2 /abc/def/ghi/Project2/2013 /abc/def/ghi/Project1/2013/file2_project1.txt /abc/def/ghi/Project2/2014 /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_255.txt /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
Desired Output:
/abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt

Replies are listed 'Best First'.
Re: perl - File::Find::Rule to exclude a single dir
by shmem (Chancellor) on Sep 17, 2014 at 20:53 UTC
    How can I frame a rule to achieve this?

    By adapting the examples in the File::Find::Rule documentation to your needs. Did you actually read the documentation? You set up a @exclude_dirs with qw(OLD). And? what for? you don't use it anywhere.

    A recipe to exclude a directory is in the documentation under the sub-heading "Further examples" following "ignore CVS directories".

    Your code looks like whipped up to provide at least something to fend off the "no code" argument; but you seem to be trying to use The Monastery as a coding service, which it is not.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: perl - File::Find::Rule to exclude a single dir
by Anonymous Monk on Sep 17, 2014 at 21:38 UTC

    You have edited your node without marking your updates. Don't do that, since now shmem's reply partially does not make sense anymore - see also this.

    Plus, your updated code still doesn't compile, making it look like you're just pasting together code snippets in the hopes that someone else will write your code for you. If you took the effort you've shown into typing up this question and put it into reading the documentation, you'd probably have a solution by now, or at least have some more cooperation (at least from me... I'm still wondering what the solution to your strptime mystery is).

      I have updated the question clearly and the code is getting compiled now. Please check and let me know incase you know the solution. strptime didnt work for me. May be because of the perl version as suggested. Thanks
Re: perl - File::Find::Rule to exclude a single dir
by Anonymous Monk on Sep 17, 2014 at 21:08 UTC

    I agree with shmem. Especially since your code doesn't compile, again. Please apply some effort to your questions, you'll get better responses, and it'll probably help you answer your own questions too.

      I have updated the question clearly and the code is getting compiled now. Please check and let me know incase you know the solution. Thanks
Re: perl - File::Find::Rule to exclude a single dir
by Anonymous Monk on Sep 18, 2014 at 02:28 UTC

    am trying to get the list of latest files in each dir(for each project) under a specific path ($output) , excluding a single dir OLD How can I frame a rule to achieve this?

    file find rule date, file find rule date, file find rule date, site:perlmonks.org file find rule date

    untested

    #!/usr/bin/perl -- use strict; use warnings; use File::Find::Rule qw/ find rule /; Main( @ARGV ); exit( 0 ); sub Main { my @dirs = ProjectDirsInExcept( $output, 'OLD' ); for my $dir (@dirs) { my ($file) = LatestFile($dir); print "$file\n"; } } sub ProjectDirsInExcept { my ( $in, @except ) = @_; my @dirs = find( directory => not_name => \@except, maxdepth => 1, in => $output, ); return @dirs; } sub LatestFile { my ($dir) = @_; my @files = find( file => maxdepth => 2, in => $dir ); @files = map { $$_[0] } sort { $$b[1] <=> $$a[1] } map { [ $_, ( stat $_ )[9] ] } @files; return shift @files; } __END__