Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

list files based on age and pattern

by kaka_2 (Acolyte)
on Sep 16, 2013 at 09:33 UTC ( #1054264=perlquestion: print w/ replies, xml ) Need Help??
kaka_2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks,

I have a function which list files from specified directory based on the regex. i want an additional filtering which should be based on time. for example which fullfill below pattern condition and file should have minimum age of 15 minute. i now i can use -M test operator to do such filtering but i am not sure if this is possible with AND operator in the same sub with grep

Could someone help me with this?

sub GetINDirFiles { my ($path) = @_; opendir DIR, $path or die $!; # my @files = readdir DIR; my @files = grep {!/\_NSA|_\d+_\d+\.xml/} readdir DIR; closedir DIR; return(@files); }
Thank for your help. -KAKA-

Comment on list files based on age and pattern
Download Code
Re: list files based on age and pattern
by hdb (Prior) on Sep 16, 2013 at 09:42 UTC

    What prevents you from trying whether it is possible or not? And should it give unexpected results, what have you tried and what did you get?

      lack of knowledge on perl syntax prevents me to do that. this is not my experties but few time i have to deal with these codes.

        To test for files older than 15 mins use ... and (-M) > 1.0 / 96 in your grep.

      i have made changes like this.
      sub GetINDirFiles { my ($path) = @_; my $min_age = 1 / 96; opendir DIR, $path or die $!; # my @files = readdir DIR; # my @files = grep {!/\_NSA|_\d+_\d+\.xml/} readdir DIR; closedir DIR; my @files = grep {!/\_NSA|d+_\d+\.xml/ && (-M) > $min_age} readdir DIR +; return(@files); }

      in the source path i added two more file and executed the code but i see @files has no file in it whereas i would expect all the files other then newly added files.

      -KAKA-

        That is, because you are doing closedir before readdir. If you were running under use warnings;, Perl would have given you an error message like

        readdir() attempted on invalid dirhandle $dh at test-m.pl line 5.
Re: list files based on age and pattern
by CountZero (Bishop) on Sep 16, 2013 at 11:52 UTC
    All that (and more) can be done using File::Find::Rule.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: list files based on age and pattern
by RichardK (Priest) on Sep 16, 2013 at 11:54 UTC

    I prefer to use File::Find::Rule for this kind of thing, it's easy to use and very flexible.

    So your query could look like this :-

    my @files = File::Find::Rule->file->name('_NSA*.xml')->mtime('<' . (t +ime() - 60 * 15) )->in($path);

      Hello Richard,

      I tried as you suggested but it gives me error "cant locate object method "file" via package file::find::rule..........

      my perl version is v5.8.8 and upgrade is not an option on client machine.

      -KAKA-

        You would need to add a line

        use File::Find::Rule;

        somewhere at the top of your script. (Assuming this module is installed on your machine.) Also, have a look at the examples in the documentation as linked in CountZero's post.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054264]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-10-25 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (147 votes), past polls