Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
rename and re-numbering
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
jvs
n00b
n00b


Joined: 01 Apr 2004
Posts: 22
Location: Utrecht (NL)

PostPosted: Mon Feb 21, 2005 9:24 am    Post subject: rename and re-numbering Reply with quote

hi,
I managed to rename filenames with spaces in it. However, the numbering part of the file starts at 117 (filename_117.jpg) till 347.
How can I reset this count starting at 001?
I tried using 'rename' or 'mv' but these file-utils don't seem to be able to do this.
thanks in advance,

jvs
_________________
"Black holes are where God divided by zero"
Back to top
View user's profile Send private message
teknomage1
Veteran
Veteran


Joined: 05 Aug 2003
Posts: 1239
Location: Los Angeles, CA

PostPosted: Mon Feb 21, 2005 10:49 am    Post subject: Reply with quote

Hi, I work with long strings of rendered frames all the time so I wrote this perl script to handle renumbering frames a while back. Maybe you could get some use out of it.
Code:
#!/usr/bin/perl -w
# take a directory, regex, starting number, ending number,
# and new starting number, and step and return a new sequence
# reseq dir '(filename.)(\d+)(\.jpg)' 48 96 24 1
$usage = 'reseq directory regex start end newStart step \n reseq dir \'(filename.)(\d+)(\.jpg)\' 48 96 24 1\n';
$dir = shift() or die "$usage";
$regex = shift() or die "$usage";
$start_num = shift() or die "$usage";
$end_num = shift() or die "$usage";
$new_start_num = shift() or die "$usage";
$step = shift() or die "$usage";

sub match_pad {
        my $old_num = shift();
        my $target_num = shift();
        my $num_length = length($old_num);
        return sprintf("%.*d", $num_length, $target_num);
}

opendir(DIR, $dir) or die "Couldn't open $dir, $!\n";
@files = sort(readdir(DIR));

$counter = 0;
foreach $file (@files) {
        if ($file =~ /$regex/) {
                my $old_number = $2;
                if ($old_number <= $end_num && $old_number >= $start_num) {
                        my $old_name = $file;
                        my $new_position = $new_start_num + ($counter * $step);
                        $new_position = match_pad($old_number, $new_position);
                        $file =~ s/$regex/$1$new_position$3/;
                        print "Moving $old_name -> $file" . "\n";
                        `mv $old_name $file`;
                        $counter++;
                }
        }
}


Okay so it looks arcane and perlish, but all you have to do is copy that whole mess to a file called 'reseq' in your path and make it executable, then you call it like it describes. The trickiest part is the regular expression argument. You have to use exactly 3 sets of parentheses and the second set has to be around the numeric portion. So to call this on your example files it'd look like
Code:
reseq . '(filename_)(\d+)(\.jpg)' 117 347 1 1
The last number is the number to count by. And please be careful especially if your data is irreplaceable it's very easy to ruin lots of files with this script.
Back to top
View user's profile Send private message
jvs
n00b
n00b


Joined: 01 Apr 2004
Posts: 22
Location: Utrecht (NL)

PostPosted: Mon Feb 21, 2005 2:53 pm    Post subject: Reply with quote

well, thanks!
It does the job, so that's enough for me.
_________________
"Black holes are where God divided by zero"
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum