Solved

Perl Script to Rename Numbered Image Files and Adding or Subtracting from the Image Name

Posted on 2007-03-28
5
239 Views
Last Modified: 2010-03-05
I have a directory of files that I want to copy to a new directory and rename the files.

The directory has images named 01_0001.tif to 01_0963.tif

and when I copy them to the new directory I need the following:

Images 1-4 need to have 3 added to them -  01_0001.tif becomes 01_0004.tif
Images 5-460 need to have 2 added to them - 01_0005.tif becomes 01_0007.tif

He is an example I am going by (we currently have a script doing the adding when the image is called,but now I just want to rename them without using the script that does the adding)

1       +3
5       +2
461     +5
704     +7

I have multiple variables like this and need to run the script against one directory at a time and the numbers above will always change.

FYI - I am not familiar enough with Perl Script to write this script, but I am familiar enough to understand what is being done and how to change the script.  We also are using an older version of Perl.  5 maybe?
0
Comment
Question by:rschooff
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 450 total points
ID: 18809588
for my $i ( reverse <01_[0-9][0-9][0-9][0-9].tif> ){
   $_=$i;
   s/(\d{4})/sprintf"%04d",$1+
   (1<=$1&&$1<=4&&3)+
   (5<=$1&&$1<=460&&2)+
   (461<=$1&&$1<=703&&5)+
   (704<=$1&&$1<=9992&&7)
   /e;
  rename $i,$_ or warn "rename($i,$_) $!"
}
0
 
LVL 39

Expert Comment

by:Adam314
ID: 18809991
If images 1-4 add 3, then image 4 becomes 7
If images 5-460 add 2, them image 5 becomes 7
Is this what you want?
Which should really be image 7?
0
 

Author Comment

by:rschooff
ID: 18811216
Ozo:
How do I tell it which directory to run on or do I place the script inside the directory where I want it to run.

Or do I make a variable that I can change for each directory:

$storage_dir = "/usr/www";
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 50 total points
ID: 18812102
To specify the directory, use

for my $i ( reverse </usr/www/01_[0-9][0-9][0-9][0-9].tif> ){
0
 

Expert Comment

by:missbarbell
ID: 18817936
For a pretty complete version try this:

---->8 cut here 8<----

#!/usr/bin/perl -w
use strict;

my $TRACE = 0;  # set to 1 to see how files are renamed

# load addition configuration
my %data;
while(<DATA>) { /^(\d+),(\d+)/ or next; $data{$1} = $2 }

# pass directory on the command or assume current directory
my $dir = $ARGV[0] || '.';

for my $source (reverse glob("$dir/*")) {
    next    unless($source =~ /tif$/);
    my $target = add($source);
    next    if($source eq $target);
    print "$source => $target\n"    if($TRACE);
    rename $source, $target
        or warn "Cannot rename $source to $target: $!\n";
}

sub add {
    my $file = shift;

    # note this does not assume only 4 character numbers
    my ($this,$zero,$that,$other) = $file =~ /^(.*)(0+)(\d+)(\.\w+)$/;
    return $file    unless($other);

    my $width = length($that);
    my $count = 0;
    for my $index (sort {$a <=> $b} keys %data) {
        last    if($index > $that);
        $count = $data{$index};
    }

    $that += $count;
    $width = length($that) - $width;

    $file = $this . substr($zero,$width) . $that . $other;
}

__END__
__DATA__
1,3
5,2
461,5
704,7

---->8 cut here 8<----

The data at the end indicates the range start value, and the increment required.

Hope that helps.

Barbie.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question