Solved

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

Posted on 2007-03-28
5
220 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
crawling gofundme 4 120
perl CPAN issue 3 99
PERL variable conundrum 9 89
Export Variables in Perl 3 45
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now