B477db7638522c078d3f0b696b85aee5

The following takes a date in the past as YYMMDD. It then processes files one month at a time from the given date to current date minus the current month.
The final product zips IIS Log files one month at a time.
It all works, but like my description, appears to be a mess.

use Time::DaysInMonth;

# Get todays date in the form of YYMMDD
(undef, undef, undef, my $dayLast, my $monthLast, my $yearLast, undef, undef, undef) = localtime(time);
$yearLast	= $yearLast - 100;	# Year is given as YYY, make it YY.

# Grap the name of the first log file (in the form of exYYMMDD.log) for our starting point and extract just the YYMMDD.
my $dateFirst = $logs[0];
$dateFirst =~ s/^ex//; $dateFirst =~ s/$.log//;

# Parse the input of YYMMDD into three separate variables.
my $yearFirst = substr($dateFirst, 0, 2);
my $monthFirst = substr($dateFirst, 2, 2);
my $dayFirst = substr($dateFirst, 4, 2);

for (my $year = $yearFirst; $year <= $yearLast; $year++) {
	if ($yearFirst == $yearLast) {
		$monthStart	= $monthFirst;
		$monthEnd	= $monthLast;
	} elsif ($year == $yearFirst) {
		$monthStart	= $monthFirst;
		$monthEnd	= 12;
	} elsif ($year == $yearLast) {
		$monthStart	= 1;
		$monthEnd	= $monthLast;
	} else {
		$monthStart	= 1;
		$monthEnd	= 12;
	} # End Months initialization.

	for (my $month = $monthStart; $month <= $monthEnd; $month++) {
		if ( ($year == $yearFirst) && ($month == $monthStart) ) {
			$dayStart	= $dayFirst;	
		} else {
			$dayStart	= 1;
		} # End Days initialization.
		$dayEnd = days_in($year + 2000, $month);

		for (my $day = $dayStart; $day <= $dayEnd; $day++) {
			#
			# Push each filename into an array.
			#
		} # End loop through Days.

		#
		# Zip The Files.
		#

	} # End loop through Months
} # End loop through Years.

Refactorings

No refactoring yet !

29cb106071d163d703484e63839d89cb

draegtun

March 2, 2009, March 02, 2009 11:50, permalink

No rating. Login to rate!

Hmmm... I did post a refactoring on this more than 3 days ago but it hasn't appeared yet ;-(

29cb106071d163d703484e63839d89cb

draegtun

March 4, 2009, March 04, 2009 10:56, permalink

No rating. Login to rate!

OK here goes again :-(

use strict;
use warnings;
use DateTime;

# example of logfiles
my @logs = qw/
    ex081205.log ex081206.log 
    ex081207.log ex090130.log 
    ex090201.log ex090202.log/;

# YYMM for last month
my $end = sprintf '%02d%02d', 
    substr( DateTime->now->year, 0 -2 ), 
    DateTime->now->subtract( months => 1 )->month;
            
my %zip;    # this will contain list of all logfile by YYYMM (key)

for my $logfile ( @logs ) {
    my ($yymm) = $logfile =~ m/^ex(\d{4})\d{2}\.log$/;
    next if $yymm > $end;
    push @{ $zip{ $yymm } }, $logfile;
}
29cb106071d163d703484e63839d89cb

draegtun

March 4, 2009, March 04, 2009 11:03, permalink

No rating. Login to rate!

Cool it worked this time ;-)

This refactoring reverses the loop... ie over the logfiles We can trust this because they come from IIS ;-)

When finished your left with a hash of arrays (%zip) which when I posted this last week (in Feb) would have looked like this....

%zip = (
    '0812' => [  'ex090130.log' ],
    '0901' => [  'ex081205.log', 'ex081206.log', 'ex081207.log' ],
);

# so current month (Feb) isn't in %zip
B477db7638522c078d3f0b696b85aee5

trirnoth

December 4, 2009, December 04, 2009 16:41, permalink

No rating. Login to rate!

I have no idea why I didn't see these as they were updated.
Last update I saw was "Hmmm... I did post a refactoring on this more than 3 days ago but it hasn't appeared yet ;-("
Possibly missed when viewing Recent Codes in Google Reader.

Actually came to the physical site for the first time in months and see where you re-posted.
Apologies and thanks for your time.
Working great!

29cb106071d163d703484e63839d89cb

draegtun

January 12, 2010, January 12, 2010 14:55, permalink

No rating. Login to rate!

No probs trirnoth. It was just annoying that RefactorMyCode swallowed my first post and made me rewrite it all again :(

And unfortunately I've just noticed a bug in my rewrite. The $end is completely naff and needs to be something like this:

my $end = do {
    my $month_ago = DateTime->now->subtract( months => 1 );
    sprintf '%02d%02d', substr( $month_ago->year, -2 ), $month_ago->month;
};

Your refactoring





Format Copy from initial code

or Cancel