Got a question that I'm having some difficulty finding - it's probably something I've missed somewhere, but I'm still essentially a newbie to Perl, so be gentle. ;)
I'm writing up a script that allows for my group to archive our shift status at the end of each shift. My manager insists on just archiving an M$ Word doc instead of letting me write up something more dynamic (which I've done before using server-parsed TCL).
My script accepts the file properly (using CGI.pm), will even warn if you are uploading a shift status that is already there and will represent the upload script but with an 'Override' field that wasn't there (set to No by default, but can be put to Yes to upload). However, I don't have direct access to the server so I can't go in and clean out files occasionally myself, and these status files can be anywhere from 100-200k, so they can fill up a directory after a month.
I'm trying to write up a function that'll go through the directory handed to it and remove the 10 oldest files when called - I've tried -M and -A both, but all files return the same age. I've included some small debug statements for my own purpose, will present here.
sub clean_dir {
my $dir = shift;
my %age;
my $debug = 1;
# Loop through files and add ages into array
opendir (DIR, $dir) or die "Unable to open $dir: $!";
while (readdir (DIR)) {
my $temp_age = -M "$dir/$_";
if ($debug) {
print "Age checked in readdir loop: $temp_age<BR>\n";
print "File checked in readdir loop: $_<BR>\n";
}
$age{$temp_age} = $_;
}
# DEBUG
if ($debug) {
print "Ages:<BR>\n";
print "$_: $age{$0}<BR>\n" foreach sort keys %age;
print "Oldest files...<BR>\n";
}
foreach (sort ({keys (%age)}[0..9])) {
print "$_[0]<BR>\n" if ($debug);
# Commented out removes to be safe for now
#system("rm", "$dir/$age{$_[0]}")
# or die "Can't remove $dir/$age{$_[0]}: $!";
print "$_[0] removed." if ($debug);
}
}
I know that if nothing else, I could just do something like:
my @files = split /\n/, `ls -rtl $dir`;
system ("rm","$dir/$_") for @files[0..9];
but that seems a little cheap - is there a better way to do this?
~Brian
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.