LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Batch/bulk rename with ascending dates? (https://www.linuxquestions.org/questions/linux-software-2/batch-bulk-rename-with-ascending-dates-721832/)

athens101 04-26-2009 01:15 PM

Batch/bulk rename with ascending dates?
 
I have molested google with this question for a few hours and came up with naught.

I have 500+ *.gif files that are ordered 1.gif 2.gif etc... I have tried the bulk rename utility in thunar and it *almost* works. I can rename the files by creation date and get them into a yyyy-mm-dd format. Problem is over a hundred files were created on the same dates. I need them all with different dates *does not matter what dates* in order to import them into a DB.

Is there some tool/script that can be given a start date and and rename the files in a ascending order?

Thanks in advance for any help with this.

Venn

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

ghostdog74 04-27-2009 03:48 AM

shell and GNU date
Code:

i=0
ls |sort -n | while read FILE
do   
    d=$(date +"%Y-%m-%d" -d "$i day")   
    echo mv "$FILE" ${d}.gif
    i=$(( i+1 ))
done


athens101 04-27-2009 06:32 PM

WOW
 
Thank you ghostdog74 and chrism01! Both solutions worked without flaw. Turned out I needed to start the image date back in 2007 but worked around that problem with Thunar bulk search and replace.

Thanks again.
Venn


All times are GMT -5. The time now is 05:16 AM.