Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-28
5
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1800 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

705 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