chrism01 |
04-27-2009 02:34 AM |
In Perl:
Code:
#!/usr/bin/perl -w
use locale; # Ensure correct charset for eg 'uc()'
use Date::Calc qw(Add_Delta_Days);
use DirHandle; # get sorted list of files to process
use strict; # Enforce declarations
my ( $today, $curr_yr, $curr_mth, $curr_day,
$next_yr, $next_mth, $next_day,
@sorted_list, $file
);
# Date calc
$today= get_todays_date();
($curr_yr, $curr_mth, $curr_day) = (substr($today, 0, 4),
substr($today, 4, 2),
substr($today, 6, 2));
@sorted_list = get_file_list(".");
for $file (@sorted_list)
{
($next_yr, $next_mth, $next_day) = Add_Delta_Days($curr_yr, $curr_mth,
$curr_day, +1);
$next_mth = (length($next_mth) < 2 ) ? "0".$next_mth : $next_mth;
$next_day = (length($next_day) < 2 ) ? "0".$next_day : $next_day;
rename($file, "${next_yr}-${next_mth}-${next_day}.gif");
($curr_yr, $curr_mth, $curr_day) = ($next_yr, $next_mth, $next_day);
}
#*****************************************************************************
#
# Function : get_file_list
#
# Description : Get list of gifs filenames in numeric order
#
# Params : none
#
# Returns : array of sorted filenames
#
#******************************************************************************
sub get_file_list
{
my $dir = shift;
my $dirh = DirHandle->new($dir) or die "can't opendir $dir: $!";
# Grab the files (gif only)
return sort grep { /\.gif$/ } # choose only gif files
$dirh->read(); # read all entries
}
#******************************************************************************
#
# Function : get_todays_date
#
# Description : Get current yr, mth, day-of-mth
#
# Params : none
#
# Returns : $today YYYYMMDD
#
#******************************************************************************
sub get_todays_date
{
my (
$year, # year ( -1900 )
$mth, # month ( -1 )
$mday, # day-of-mth
$today # today string: yyyymmdd
);
# Get today
# NB: Mth array starts at zero
($mday, $mth, $year) = (localtime)[3..5];
$today = sprintf( "%04d%02d%02d", $year+1900, $mth+1, $mday );
return $today;
}
a bit quick and dirty, but works for me
input
Code:
1.gif 2.gif 3.gif 4.gif 5.gif
output
Code:
2009-04-28.gif 2009-04-30.gif 2009-05-02.gif
2009-04-29.gif 2009-05-01.gif
note that in this form you put the perl prog in the dir containing the gifs
|