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

Hi Monks!
I need to delete files(sessions), if they are left by the user and if they are more the 5 minutes old. I just cant get the -M operator to work, any help?
... opendir(DIR, $all_session) or die "Couldn't open directory, $!"; while (my $sessions = readdir DIR) { # Use a regular expression to ignore files beginning with a peri +od next if ($sessions =~ m/^\./); next if ($sessions !~ /^cgisess_[a-f0-9]{32}$/); if( -M qq{$all_session/$sessions} > 360){ # 6 minutes print "Del these session files: $sessions<br>";} } closedir DIR; ...
Thanks for the help!

Replies are listed 'Best First'.
Re: Delete on age of files Help!
by toolic (Bishop) on Jan 16, 2011 at 03:15 UTC
      Hi, even this doesnt work:
      #!/usr/bin/perl -w use strict; use File::stat; my $mytime=(time); my $file_stat; ... opendir(DIR, $all_session) or die "Couldn't open directory, $!"; while (my $sessions = readdir DIR) { # Use a regular expression to ignore files beginning with a peri +od next if ($sessions =~ m/^\./); next if ($sessions !~ /^cgisess_[a-f0-9]{32}$/); #if( -M qq{$all_session/$sessions} > 360){ # 6 minutes #print "Del these session files: $sessions<br>"; #} if ($file_stat = (stat $sessions)[9]){ if ($file_stat >= ($mytime - 300)) { print "File older than 5min found. The file name is $ses +sions"; last; } } } closedir DIR; ...
        stat returns something different:
        stat[ 9 ] is an absolute time (from epoch).
        Perl: Stat
      I guess this line should work:
      if( -M qq{$all_session/$sessions} > 0.004){ print "del this $sessions +";} %6 minutes
      How did you get 0.004?
        How did you get 0.004?
        1 day = 1440 minutes (24hours/day x 60minutes/hour)
        6/1440 = 0.004 (roughly)
Re: Delete on age of files Help!
by Anonymous Monk on Jan 16, 2011 at 04:16 UTC