1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#!/usr/bin/perl
#
# Program Summary:
#
# Name: dailystrips-clean
# Description: removes all downloaded dailystrips in the current directory
# that are older than a specified number of days
# Author: Andrew Medico <amedico@amedico.dhs.org>
# Created: 12 Feb 2002, 21:44 EST
# Last Modified: 21 Jun 2002, 03:28 EDT
# Current Revision: 1.0.1
#
# Set up
use strict;
no strict qw(refs);
use POSIX qw(strftime);
use Getopt::Long;
# Variables
my (%options, $version, $time_today, @files);
$version = "1.0.1";
$time_today = time;
# Get options
GetOptions(\%options, 'quiet|q','verbose|v','test|t','dir=s','archive|a','version|v','help|h')
or exit 1;
# Help and version override anything else
if ($options{'help'}) {
print
"Usage: $0 [OPTIONS] DAYS
DAYS is the number of days to keep.
Options:
-q --quiet Turn off progress messages
-v --verbose Turn on extra progress information, overrides -q
-t --test Do not actually remove files
-d --dir DIR Work in specified directory instead of current
directory
-a --archive Update archive.html file
-V --version Print version number
-h --help Print this help
Bugs and comments to dailystrips\@amedico.dhs.org\n";
exit;
}
if ($options{'version'}) {
print "dailystrips-clean version $version\n";
exit;
}
unless (defined $ARGV[0]) {
die "Error: no number of days specified\n";
} else {
$options{'days'} = $ARGV[0];
if ($options{'days'} =~ m/\D/) {
die "Error:number of days must be numeric\n";
}
if ($options{'days'} =~ m/\D/) {
die "Error:number of days must be numeric\n";
}
}
# verbose overrides quiet
if ($options{'verbose'} and $options{'quiet'}) {
undef $options{'quiet'};
}
# get list of existing files
if ($options{'dir'} and (not $options{'dir'} =~ /\/$/)) {
$options{'dir'} .= "/";
}
@files = grep(/\d{4}\.\d{2}\.\d{2}/, glob("$options{'dir'}*"));
for (@files) {
if ($options{'verbose'}) {
print "Existing file: $_\n";
}
}
# filter out files to keep
for (0 .. $options{'days'} - 1) {
my $save_day = strftime("\%Y.\%m.\%d", localtime ($time_today - (86400 * $_)));
unless ($options{'quiet'}) {
print "Keeping files for: $save_day\n";
}
@files = grep(!/$save_day/, @files);
}
# remove anything that's still on the list
for (@files) {
if ($options{'verbose'}) {
print "Removing file/directory: $_\n";
}
unless ($options{'test'}) {
if (-d $_) {
my $dir_not_empty;
foreach my $sub (glob("$_/*")) {
unless (unlink("$sub")) {
warn "Could not remove file $sub: $!\n";
$dir_not_empty = 1;
}
}
if ($dir_not_empty) {
warn "Directory $_ not empty, cannot remove\n";
} else {
rmdir($_) or warn "Could not remove directory $_: $!\n";
}
}
else {
unlink($_) or warn "Could not remove file $_: $!\n";
}
}
}
if ($options{'archive'})
{
if (open(ARCHIVE,"<$options{'dir'}archive.html"))
{
my $oldest = strftime("\%Y.\%m.\%d", localtime ($time_today - (86400 * ($options{'days'}-1))));
my $out;
while(<ARCHIVE>)
{
if (/(\d{4}\.\d{2}\.\d{2})/)
{
if ($1 lt $oldest)
{
$_ = "";
}
}
$out .= $_;
}
close(ARCHIVE);
if (open(ARCHIVE,">$options{'dir'}archive.html"))
{
print ARCHIVE $out;
}
else
{
warn "Error: cannot update archive.html - could not write file: $!\n";
}
}
else
{
warn "Error: cannot update archive.html - could not read file: $!\n";
}
}
|