How about something like:
#!perl -w
use strict;
use File::Find;
my $days = 12 * 7; # twelve weeks, seven days a week.
sub DeleteOldFiles
{
return 0 unless -M > $days;
unlink $_ or die $!;
return 1;
}
find( \&DeleteOldFiles, '.' );
Although I think that also sweeps sub directories too.
Update
Hmmm, File::Find is really for delving into the subdirs, and if you don't want that, then just glob(*) like this:
#!perl -w
use strict;
my $days = 12 * 7; # twelve weeks, seven days a week.
while( <*> )
{
next if -d;
unlink $_ or die $! if -M > $days;
}
By the way, -M returns days since last modified, -A returns days since last accessed. I wasn't sure what your purpose was, but recently accessed files might be useful.
As for speed issues, you have to loop over everyfile, no matter what, the only question is the efficiancy of the loop. Well, do as little as possible in the loop. Calculate the age aforehand, and short circuit where you can.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.