I have a hard time writing a script that look at file and directories time and date stamp then delete any thing that is so many days old. if someone would give me a good starting point.Some time ago i had the same problem, so here what i came up with. This script includes some options, can be used interactively or with a configuration file, can be recursive or not, and so on. I hope the documentation included in the script could be enough.
HTH
-- Francesco
# FILE: clean.pl # NAME: clean old files # VERSION: 0.3 # AUTHOR: abeni # MODIFIED: 01/12/2004 # DESCRIPTION: it deletes files older than X days in a given folder. # Parameters can be given from command line: # "clean.pl -d folder1 days1 folder2 days2" # or with an external configuration file: # oppure tramite un file di configurazione: # "clean.pl -f configfile" # The configuration file should have on each row: # "folder days" # For other parameters run clean.pl -h
use strict; use Getopt::Std;
sub clean; # procedura di cancellazione vera e propria sub usage; # sub che illustra la sintassi del programma sub cmd_line; # sub che reperisce i parametri da linea di comando sub cfg_file; # sub che reperisce i parametri da file di configurazione sub interact; # sub che richiede i parametri in modalita' interattiva
(@ARGV > 0) or die usage;
my %days = (); # hash con nomi directory e giorni di anzianita' dei file da cancellare
my $dir;
my $progdir = "d:/perls/prog/clean/";
my $logfile = $progdir."clean.log";
my %opts;
### PRINCIPALE ##############
# analizza gli switch passati al programma ed elabora di conseguenza i parametri
getopts('rd:f:h',\%opts);
SWITCH: {
$opts{d} && do {%days=split/,/,$opts{d}; last SWITCH};
$opts{f} && do {my $file = $opts{f}; %days=cfg_file($file); last SWITCH};
$opts{h} && usage; };
open (LOG,">> $logfile") or die "Impossibile accedere al file di log $logfile: $!";
print LOG "\n\n***".scalar(localtime)."\n\n";
# avvia la cancellazione ogni cartella passata come parametro foreach $dir (keys %days) { my $filecount = 0; print LOG "Cartella: $dir\nGiorni: $days{$dir}\n"; $filecount += clean($dir,$days{$dir}); print LOG "Totale file cancellati: $filecount\n\n"; }
print "End";
### CLEAN #########
sub clean {
my $dir = shift;
my $days = shift;
my $filecount = 0;
opendir (DIR,$dir) or do {print LOG "Impossibile aprire la cartella $dir: $!\n\n";
next;};
foreach my $file (readdir DIR) {
next if $file =~ /^\.*$/;
if (-d "$dir\\$file") {
if ($opts{r}) {
$filecount += clean("$dir\\$file",$days);
rmdir "$dir\\$file" or print LOG "Impossibile cancellare la cartella $dir: $!\n";
};
next;
};
next unless (-M "$dir\\$file" > $days);
unlink ("$dir\\$file") or print LOG "Impossibile cancellare il file $dir\\$file: $!\n";
$filecount++;
}
closedir DIR;
return $filecount;
}
### USAGE ######### sub usage { print "\nSyntax:\n"; print " $0 -d \"[dir1],[days],[dir2],[days]...\" [-r]\n"; print " $0 -f [config file] [-r]\n"; print "\nThe configuration file has the following format:\n"; print " dir1, days\n"; print " dir2, days\n"; print " ...\n\n"; print "If you choose -f option without specifying a configuration file,\n"; print "the script looks for a \"clean.ini\" in the script folder\n"; print "If you choose both -d and -f, the -d option will prevail\n"; print "and command line parameters will be used.\n\n"; print "The -r option enable recursive deletion (go into subfolders)\n\n" print "To show this help:\n"; print " $0 -h\n"; exit; }
### CFG_FILE
############
sub cfg_file {
my $file = (shift or $progdir."clean.ini");
my %days;
open (FILE, $file) or die "Can't open configuration file $file: $!";
while (<FILE>) {
next if /^#/;
next unless /\S/;
chomp;
# verifica il formato
die "Errori nel file di configurazione: $_\n" unless (/([^\,]+)\,(\d+)$/);
$days{$1} = $2;
}
return %days;
}
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>