Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: list files based on age and pattern

by kaka_2 (Sexton)
on Sep 16, 2013 at 13:18 UTC ( #1054293=note: print w/replies, xml ) Need Help??

in reply to Re: list files based on age and pattern
in thread list files based on age and pattern

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.


Replies are listed 'Best First'.
Re^3: list files based on age and pattern
by hdb (Monsignor) on Sep 16, 2013 at 13:24 UTC

    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.
      Hello hdb,

      Sorry it is mistake while copy paste but in the actual script closdir is after the readdir.

      there is no syntax or compilation error, only no files.


        My fault. You need to add the path to the filename when testing with -M like this

        my @files = grep {!/\_NSA|d+_\d+\.xml/ && (-M "$path/$_") > $min_age} readdir DIR;

        I only tested in the current directory where it does not make any difference.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054293]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2018-01-18 14:43 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (212 votes). Check out past polls.