Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Using -M to test age of file

by bwelch (Curate)
on Aug 22, 2003 at 15:03 UTC ( #285804=perlquestion: print w/ replies, xml ) Need Help??
bwelch has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble using "$fileAge = -M $fileName" to determine age of files in a directory. Basically, I need to see if all the files in a directory are more than a day old. If they are, I process them. My problem is that whenever the fileAge is printed out, it's null and looks like this: "Filename=[result.896] file age = []" I should add that all the files I'm currently scanning are less than a day old for now. What am I doing wrong?

# Test output files for age to see if they should be processed # Start out assuming all output files may be parsed my $filesAreOldEnough = 1; if ( $numOutputFiles > 0 ) { foreach my $fileName ( @outputFiles ) { my $fileAge = -M $fileName; print "Filename=[ $fileName ] file age = [ $fileAge ]\n"; if ( $fileAge < 1 ) { $filesAreOldEnough = 0; #last; # check all files for now } } } else { # No input files to process, skip directory... $filesAreOldEnough = 0; }

edited by ybiC: URL-escaped square brackets in text

Comment on Using -M to test age of file
Download Code
Re: Using -M to test age of file
by tcf22 (Priest) on Aug 22, 2003 at 15:17 UTC
    Try using stat

    Change
    my $fileAge = -M $fileName;
    to
    my $fileAge = (stat($filename))[9];


    Update: Looks like it is probably the path being wrong. Apparently these commands do pretty much the same thing internally(which is probably good to know), so just ignore my original answer.

      Try using stat

      Why? You seem to presume that stat and -M are not intimitely related? In fact -X just calls stat and caches the unused result fragments in the _ magical var - yes it is called _ so you can do this:

      $exist = -e $blah; # calls stat, returns -e , caches result $dir = -d _; # takes the last stat (ie $blah) returns -d value $file = -f _; # etc

      The problem is a path issue, the solution is:

      my $fileAge = -M $fileName || die "Can't stat $fileName $!\n"

      The error will be No such file or directory Solution full path. Yes you can make relative work but that is an exercise for the reader.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        In fact -X just calls stat and caches the unused result fragments in the _ magical var - yes it is called _ so you can do this:

        Just to clarify, caching the results isn't peculiar to the filetest operators; stat() itself caches the results too.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Using -M to test age of file
by VSarkiss (Monsignor) on Aug 22, 2003 at 15:23 UTC

    This is just a guess, but... Are you either pre-pending the directory name to the file name, or changing to the directory? The symptom sounds like you're doing something like:

    foreach my $dir (@directories) { foreach my $file (@filenames) { $age = -M $file; ....
    When what you should be doing is:
    foreach my $dir (@directories) { foreach my $file (@filenames) { $age = -M "$dir/$file"; # see the difference? ....
    or something like this:
    foreach my $dir (@directories) { chdir $dir or die "Couldn't chdir to $dir: $!\n"; foreach my $file (@filenames) { $age = -M $file; .... } chdir ".." or die "Help, I stranded myself";

    As I said, just a guess. HTH.

Re: Using -M to test age of file
by tachyon (Chancellor) on Aug 22, 2003 at 15:23 UTC

    You need the full path to the file. If you checked for the error with -M $fileName || die "Can't stat $fileName, Perl say $!\n" it would say No such file or directory You can check errors in $! for all sorts or perl functions open, mkdir, unlink and -X are just a few where it may (or may not) be useful.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Using -M to test age of file
by arthas (Hermit) on Aug 22, 2003 at 15:29 UTC

    Hi!

    I did cut&paste your code on my editor and executed it (after filling @outputFiles with something) and it works:

    Filename=[ prova.pl ] file age = [ 2.31481481481481e-05 ] Filename=[ save.pl ] file age = [ 333.155497685185 ]

    Maybe the problem is that you don't specify the full path. By the way, you could change:

    if ( $numOutputFiles > 0 )

    with:

    if ( @outputFiles > 0 )

    In scalar context, @array returns the number of its elements.

    Michele.

Re: Using -M to test age of file
by bwelch (Curate) on Aug 22, 2003 at 16:46 UTC
    Thanks much for the help! I kept looking in the wrong places for the problem...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://285804]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-09-01 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (8 votes), past polls