CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
best practice for checking how old a file is

 



gerble1000
User

Oct 13, 2012, 1:06 PM

Post #1 of 6 (1234 views)
best practice for checking how old a file is Can't Post

i need to have my script check a file and see if it is over a week old. basicly i have a script that has a feature in it that i only want to excute if the feature hasnt been used in a week.
so i thought of printing the date inside a file and then the script will calculate if it has been a week or more and if so execute the script.
whats the best way to do this


BillKSmith
Veteran

Oct 13, 2012, 2:00 PM

Post #2 of 6 (1230 views)
Re: [gerble1000] best practice for checking how old a file is [In reply to] Can't Post

I like your idea to keep the time in the filen because the times returned by the file test operators are subject to change beyond your control. You can use the result of the time (Refer: perldoc -f time) function without any modules. A file is more than a week old if the difference between the current time and the saved time is greater than 604800 (60 x60 x 24 x 7) seconds.
Good Luck,
Bill


gerble1000
User

Oct 13, 2012, 2:21 PM

Post #3 of 6 (1227 views)
Re: [BillKSmith] best practice for checking how old a file is [In reply to] Can't Post

would this be the way you would choose to stop the execution of the code unless its been over a week


neil4636
New User

Oct 28, 2012, 11:52 AM

Post #4 of 6 (1074 views)
Re: [gerble1000] best practice for checking how old a file is [In reply to] Can't Post

Do you need to check a folder or just a specific file?


neil4636
New User

Oct 28, 2012, 11:56 AM

Post #5 of 6 (1073 views)
Re: [neil4636] best practice for checking how old a file is [In reply to] Can't Post


Code
#!/usr/bin/perl 

print "Content-type: text/html\n\n";

use strict;
use File::Find;

if ($ARGV[0] eq "") { $ARGV[0]="."; }

my @file_list;
find ( sub {
my $file = $File::Find::name;
if ( -f $file && $file =~ /^DATE_/) {
push (@file_list, $file)
}
}, @ARGV);

my $now = time(); # get current time
my $AGE = 60*60*24*7; # convert 7 days into seconds

for my $file (@file_list) {
my @stats = stat($file);
if ($now-$stats[9] > $AGE) { # file older than 14 days
print "$file\n";
}
}



neil4636
New User

Oct 28, 2012, 11:58 AM

Post #6 of 6 (1072 views)
Re: [neil4636] best practice for checking how old a file is [In reply to] Can't Post

You could just make a temp file - and use the above script to check if the file is 1 week old?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives