Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Checking if file has been modified in last 7 days using stat [9]

by drock (Beadle)
on Mar 17, 2005 at 22:37 UTC ( #440569=perlquestion: print w/replies, xml ) Need Help??

drock has asked for the wisdom of the Perl Monks concerning the following question:

ok perl'ers... I want to stat a file and element 9 I am interested in. But this returns since epoch so how can I say "check to see if file was modified +7 days or 60*60*24*7? thanks, derek
snip.... my @info = (stat ($tapes))[9]; print "@info\n";

Edit by BazB: change title from "stat [9]".

Replies are listed 'Best First'.
Re: Checking if file has been modified in last 7 days using stat [9]
by Tanktalus (Canon) on Mar 17, 2005 at 23:47 UTC

    In the spirit of TMTOWTDI ... and in the spirit of "How do I do X with Y", I present: "How do I do X ... with X?":

    my @info = stat $tapes; print 'this file hasn't been modified in a while' if -M _ > 7;

    The -M function was written specifically for this - as long as you have a short-running script. That's becasue -M compares the modification time to the time that your script started. If you have something running in a persistant environment (e.g., mod_perl), this is probably not what you want. Cron jobs, however, would be somewhere that this just makes things easier.

Re: Checking if file has been modified in last 7 days using stat
by BazB (Priest) on Mar 17, 2005 at 22:51 UTC

    I'm not going to give you the code, but the method should be something like:

    • Get current time since epoch (hint: time())
    • Get mtime of file in question, since epoch
    • Check if current time minus mtime is less or more than the number of seconds in 7 days.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: Checking if file has been modified in last 7 days using stat
by friedo (Prior) on Mar 17, 2005 at 22:54 UTC
    First, since you're only getting one element from stat, you might as well stick it in a scalar instead of an array. To see if a file was modified more than seven days ago, just get the current time and compare.

    my $mtime = (stat $tapes)[9]; my $now = time; if( $mtime < ( $now - 604800) ) { print "$tapes is more than seven days old.\n"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2023-02-04 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?