There is more than one way to do this, and the "correct" way depends on the minute details of your setup. Here's the code i use in my framework (this isn't exactly from the most modern module i have, but it works):

# Use the object oriented version of stat use File::stat; ... sub _dircleaner($self, $dir, $maxagedays) { my $reph = $self->{server}->{modules}->{$self->{reporting}}; $reph->debuglog("Scanning $dir for cleaning"); my @todelete; my $deletes = 0; my $ok = 1; my $dfh; if(!opendir($dfh, $dir)) { $reph->dblog("DIR_CLEANER", "Can't open '$dir'"); $dbh->commit; $reph->debuglog("Can't open $dir"); $ok = 0; goto finishcleaning; } my $fcount = 0; my $maxage = $maxagedays * 3600 * 24; # Convert days to seconds my $now = time(); while((my $fname = readdir($dfh))) { next if($fname eq "." || $fname eq ".."); # FIXME FOR SUBDIRS! CLEAN UP SUBDIRS, THEN REMOVE ALL EMPTY D +IRS next if(!-f "$dir/$fname"); my $fileage = stat("$dir/$fname")->mtime; my $age = $now - $fileage; next if($age <= $maxage); push @todelete, "$dir/$fname"; $fcount++; } closedir($dfh); if($fcount) { $reph->debuglog("Cleaning $fcount file(s) in $dir"); foreach my $fname (@todelete) { if(unlink $fname) { $deletes++; $reph->debuglog(" ...deleted $fname"); } else { $ok = 0; $reph->debuglog("Failed to delete $fname"); } } $reph->debuglog("Deleted $deletes file(s)."); } finishcleaning: return $ok; }

Basically, i read the whole directory in and remember the files i want to delete, then delete them one-by-one. The relevant steps for filtering the files is:

# Convert age (in days) to seconds my $maxage = $maxagedays * 3600 * 24; # Convert days to seconds # get the "last modified" timestamp of the file in SECONDS (unix times +tamp) my $fileage = stat("$dir/$fname")->mtime; # Current time in seconds (unix timestamp) my $now = time; # Age in seconds my $age = $now - $fileage; # Ignore file if it is too new next if($age <= $maxage); # Add files to the "@todelete" list with full filename push @todelete, "$dir/$fname";


In reply to Re: unlink files by cavac
in thread unlink files by frank1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.