1

I have the below shown directory structure.

  1. I would like to get just the project names in my @project. @project=('project1','project2');
  2. I would like to exclude OLD directory and its sub directories in @project
  3. I would like to get latest file in the subdirectories for all projects in @project. i.e., for project1, latest file is in sub directory 2014, which is foobar__2014_0916_248.txt

How can I frame a rule to achieve this?

use strict;
use File::Find::Rule;
use Data::Dump;
my $output       = "/abc/def/ghi";
my @exclude_dirs = qw(OLD);
my @projects     = File::Find::Rule->directory->in("$output");
dd \@projects;

My Dir Structure:

.
├── project1
│   ├── 2013
|        ├── file1_project1.txt
│   └── 2014
|         ├── foobar__2014_0912_255.txt
|         ├── foobar__2014_0916_248.txt
├── project2
│   ├── 2013
|        ├── file1_project2.txt
│   └── 2014
|         ├── foobarbaz__2014_0912_255.txt
|         ├── foobarbaz__2014_0916_248.txt
└── OLD
    └── foo.txt
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Jill448
  • 1,745
  • 10
  • 37
  • 62

1 Answers1

3

As ikegami suggested, just do this in two steps.

  1. First find your project names
  2. Second find the latest file

The following does this using Path::Class and Path::Class::Rule

use strict;
use warnings;
use autodie;

use Path::Class;
use Path::Class::Rule;

my $testdir = dir('testing');

for my $project ( $testdir->children ) {
    next if !$project->is_dir() || $project->basename eq 'OLD';

    my $newest;

    my $next = Path::Class::Rule->new->file->iter($project);
    while ( my $file = $next->() ) {
        $newest = $file if !$newest || $file->stat->mtime > $newest->stat->mtime;
    }

    print "$project - $newest\n";
}

Outputs:

testing/project1 - testing/project1/2014/foobar__2014_0916_248.txt
testing/project2 - testing/project2/2014/foobarbaz__2014_0916_248.txt
Miller
  • 34,962
  • 4
  • 39
  • 60
  • I couldnt install Path::Class::Rule in my system . Tried from `cpan> install Path::Class::Rule` . It gave an error `Warning: Cannot install Path::Class::Rule;, don't know what it is.` . Tried `curl -k -L http://cpanmin.us | perl - --sudo Path::Class` which throwed an error too – Jill448 Sep 17 '14 at 21:03
  • It's not required to use `Path::Class`, I simply like that branch of modules since it easily enables cross platform file system handling. You could easily iterate over your base directory using a different method and search a project tree using `File::Find::Rule`. It just might take more code. – Miller Sep 17 '14 at 21:26
  • If you want to explore why you can't install those modules, perhaps start a new question as the error message your getting doesn't make sense. – Miller Sep 17 '14 at 21:27
  • Did it...posted another question..Thanks – Jill448 Sep 17 '14 at 21:55
  • @sravs448 Your new question has nothing to do with your error installing `Path::Class::Rule`. Oh well. – ThisSuitIsBlackNot Sep 17 '14 at 21:58
  • 1
    @sravs448 I just noticed, it looks like you ran `install Path::Class::Rule;` (with a semi-colon) from the CPAN shell. Drop the semi-colon, or simply run `cpan install Path::Class::Rule` from the command line. – ThisSuitIsBlackNot Sep 17 '14 at 22:04
  • surely will try that. Here is the link for my new question. Let me know incase you know the solution. Thanks . `http://stackoverflow.com/questions/25900989/perl-filefindrule-to-find-latest-file-in-directories` – Jill448 Sep 17 '14 at 22:09