#!/usr/bin/perl -w

use strict;
use File::stat;
my $startingdir="/tmp"; # could use $ARGV[0] here to make it more
my $cutoff=5;                    # could also pass in the cutoff from

&Purge($startingdir);

########## Subroutines #############

sub Purge {
  (my $dir)=@_;

### this is more normally written as
###   my ($dir) = @_;
### or
###   my $dir = shift;
### putting the parens outside the my declaration can cause problems:
###   (my $x, $y) = (1,2);
### the $x is my()d, but the $y is not


  my @ls=`ls -A $dir`;           # All files except . and ..

### ick -- use opendir() and readdir(), or glob()

  foreach my $file (@ls) {
    chomp $file;
    my $date= ( -M "$dir/$file" ); # -M = age of file (at startup) in
    if ($date>$cutoff) {           # If the file/directory is greater

### you don't need a variable to store the return value of -M
### just use it
###   if ((-M "$dir/$file") > $cutoff) { ... }

      print "Purging $dir/$file\n";
      system("rm -rf $dir/$file");   # Delete recursively and force it
    } elsif ( -d "$dir/$file" ) {  # otherwise if it's a directory ...
      print "Recursing into $dir/$file ...\n";
      &Purge("$dir/$file");          # Recurse into it
    }
  }
}

__END__


-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to