I take it that 20111031 is the date of that log file. And it appears that the logging software starts a new log file each day? So, all you have to do is move the old log files to some archive directory if it is from yesterday. If that's not right, then please explain.
This type of string "20111031" is great as long as it has leading zeroes for the months and years as that can be used in a simple string comparison against the current date string.
So how to get the current date string?
#!/usr/bin/perl -w
use strict;
my ($year,$month,$day) = (gmtime(time))[5,4,3];
$year +=1900;
$month +=1;
my $date = sprintf("%4d%02d%02d", $year,$month,$day);
print "date = $date\n"; #date = 20111031
Extract the date string from the filename using a regex, then use a simple string comparison and then move the file it if is less than today's date. Here's how to extract the date string..
#!/usr/bin/perl -w
use strict;
while (<DATA>)
{
#one of many, many ways to get the 8 digit date
my ($date) = /(\d{8}).*.log$/;
print "$date\n";
}
=prints
20111031 // Oct 31, 2011
20110101 // Jan 01, 2011
=cut
__DATA__
AAAA*20111031*.log
AAAA*sfdaf*20110101*.log
I suppose from the formulation of the problem that you are thinking in terms of Windows wildcards and not UNIX regular expressions. Experiment with the above code and I think it will work for you. Report back if you have problems. |