braintoast has asked for the wisdom of the Perl Monks concerning the following question:
I have a feeling this is either going to be easy or impossible.
I need to:
-read a dir
-find the ten newest files in it
-delete any others
I'm relatively sure that what I have below will remove anything older than 10 days, but I need to keep *10 days worth* of files. (Since this'll be run by cron daily, I have a problem on weekends or any other day when nothing is added to the directory..I fall below my mark of retaining the 10 *most recent* files.)
#!/usr/local/bin/perl -w use strict; use vars qw($archivedir @subdirs $subdirs); $archivedir = "/web/1/someplace" ; # READ CONTENTS OF DIRECTORY AND DELETE FILES OLDER THAN 10 DAYS opendir (ARCHDIR, "$archivedir") || die "Couldn't access the directory +!"; @subdirs = readdir(ARCHDIR); foreach $subdirs (@subdirs) { if (-M "$subdirs" > 10) { rmdir("$archivedir/$subdirs"); } } closedir ARCHDIR;
Ideas? Feel free to berate me if this turns out to be easy or my code looks silly. I'm still have tons to learn and welcome *any* advice.
Thanks!
|
---|
Back to
Seekers of Perl Wisdom