Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

list files based on age and pattern

by kaka_2 (Sexton)
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-

Replies are listed 'Best First'.
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.


    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 (Parson) 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.


        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.

Re: list files based on age and pattern
by hdb (Monsignor) 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?

      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.


        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 line 5.

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054264]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-11-23 09:47 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (333 votes). Check out past polls.