LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > 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
  Search this Thread
Old 04-26-2009, 01:15 PM   #1
athens101
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Rep: Reputation: 15
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
 
Old 04-27-2009, 02:34 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,241

Rep: Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713Reputation: 2713
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
 
Old 04-27-2009, 03:48 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 04-27-2009, 06:32 PM   #4
athens101
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:42 AM
shell command for bulk 'rename' anon201 Linux - Newbie 5 12-17-2008 03:46 AM
Batch rename troubles djeikyb Linux - General 4 07-04-2007 02:38 PM
Batch Rename shelfitz Linux - Newbie 19 10-12-2006 10:23 PM
Batch File for format? Bulk Formatting Beebe Solaris / OpenSolaris 2 04-12-2005 05:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration