djw has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; # uuuuussseeee thiiiissss use File::stat; # need this for ctime (creation time using sec +onds since epoch) use File::Find qw(finddepth); # need this for recursive directory my($create_date, $current_date, $time_month, $month_old); my($dir,$dirs,$logfile,$logfile2); ############################## # good little purl h4x0r's # # define their variable's # ############################## $_ = *File::Find::name; $month_old = 2592000; # set this to seconds. 2592000 = 30 d +ays $current_date = time; # gives current time since the epoch +in seconds $dir = 'c:\temp\foo'; $logfile = 'c:\temp\log.txt'; $logfile2 = 'd:\temp\log_keep.txt'; ######################################## # The switch is flipped! # # using File:Find:finddepth # # to step through all subdirectories # # using my function to test and delete # ######################################## finddepth \&deletion, $dir; ############################################# # in my sub I start with files then # # directories. Each creation date is # # tested before the nested if statements # # are run so we can see if the data needs # # deletion or not (oder than 30 days). # # I also log all deletion and non deletion # # activity # ############################################# sub deletion { if (-f) { $create_date = stat($_)->ctime; if ($create_date < ($current_date - $month_old)) { unlink ($_) or warn "Cannot delete file: $_ ($!)"; open(LOG, ">>$logfile") or warn "discarding logfile output +\n"; print LOG "FIle: $_ - has been deleted.\n"; close (LOG) or warn "Can't close: $!"; } else { open(LOG, ">>$logfile2") or warn "discarding logfile outpu +t\n"; print LOG "The file $_ is newer than 30 days.\n"; close(LOG) or warn "Can't close: $!"; } } else { $create_date = stat($_)->ctime; if ($create_date < ($current_date - $month_old)) { rmdir ($_) or warn "Cannot delete directory: $_ ($!)"; open(LOG, ">>$logfile") or warn "discarding logfile output +\n"; print LOG "Directory: $_ - has been deleted.\n"; close (LOG) or warn "Can't close: $!"; } else { open(LOG, ">>$logfile2") or warn "discarding logfile outpu +t\n"; print LOG "The dir $_ is newer than 30 days.\n"; close(LOG) or warn "Can't close: $!"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A couple of problems....
by chromatic (Archbishop) on Oct 18, 2000 at 23:52 UTC | |
by djw (Vicar) on Oct 19, 2000 at 00:28 UTC | |
|
RE: A couple of problems....
by lachoy (Parson) on Oct 19, 2000 at 01:36 UTC | |
|
Re: A couple of problems....
by wardk (Deacon) on Oct 19, 2000 at 01:23 UTC | |
|
RE: A couple of problems....
by knight (Friar) on Oct 19, 2000 at 00:21 UTC | |
by djw (Vicar) on Oct 19, 2000 at 00:25 UTC | |
|
Re: A couple of problems....
by djw (Vicar) on Oct 19, 2000 at 20:10 UTC | |
|
suggestion
by ColonelPanic (Friar) on Oct 20, 2000 at 01:49 UTC |