LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Thread Tools
Old 04-26-2009, 02:15 PM   #1
athens101
LQ Newbie
 
Registered: Aug 2004
Posts: 26
Thanked: 0
Batch/bulk rename with ascending dates?


[Log in to get rid of this advertisement]
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
athens101 is offline     Reply With Quote
Old 04-27-2009, 03:34 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.3
Posts: 7,283
Thanked: 311
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
chrism01 is offline     Reply With Quote
Thanked by:
Old 04-27-2009, 04:48 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,674
Blog Entries: 5
Thanked: 88
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
ghostdog74 is offline     Reply With Quote
Thanked by:
Old 04-27-2009, 07:32 PM   #4
athens101
LQ Newbie
 
Registered: Aug 2004
Posts: 26
Thanked: 0

Original Poster
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
athens101 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bulk rename files atiman Linux - Newbie 5 03-05-2009 01:42 AM
shell command for bulk 'rename' ashrafulkarim Linux - Newbie 5 12-17-2008 04:46 AM
Batch rename troubles djeikyb Linux - General 4 07-04-2007 03:38 PM
Batch Rename shelfitz Linux - Newbie 19 10-12-2006 11:23 PM
Batch File for format? Bulk Formatting Beebe Solaris / OpenSolaris 2 04-12-2005 06:07 AM


All times are GMT -5. The time now is 11:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration