Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / June 2005



Tip: Looking for answers? Try searching our database.

Newbie: renaming files

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marek Stepanek - 11 Jun 2005 13:26 GMT
Hello all,

Sorry for this beginners question. I have many files which I want to rename
as follows:

$src            =>      $dest

file01.jpg      =>      file01_01.jpg
file02.jpg      =>      file01_02.jpg
file03.jpg      =>      file01_03.jpg
file04a.jpg     =>      file01_04a.jpg
file04b.jpg     =>      file01_04b.jpg
file05.jpg      =>      file01_05.jpg
file06a.jpg     =>      file01_06a.jpg
file06b.jpg     =>      file01_06b.jpg
file06c.jpg     =>      file01_06c.jpg
file06d.jpg     =>      file01_06d.jpg
file07.jpg      =>      file01_07.jpg
file08.jpg      =>      file01_08.jpg
file09.jpg      =>      file01_09.jpg
file10.jpg      =>      file01_10.jpg
file11.jpg      =>      file01_11.jpg
file12.jpg      =>      file01_12.jpg
file13.jpg      =>      file02_01.jpg
file14.jpg      =>      file02_02.jpg
...
file24.jpg      =>      file02_12.jpg
file25.jpg      =>      file03_01.jpg

What I have so far is the following script :

#!/usr/bin/perl -w

use strict;
use warnings;

for my $src (@ARGV) {
   next unless $src =~ /^(file)(\d+[a-f]?)(\.jpg)$/i;
   my ($root, $nr, $ext) = ($1, $2, $3);
   next unless (($nr =~ /(d+)/) and ($1 > 12)) ; # here is my big problem !
       my $dest = lc ("$root" . "01_" . "$nr$ext");
       
       if(-e $dest) {
           warn "$dest already exists. Skipping ...\n";
           next;
       }
       print "Renaming would rename the following files \n
$src\t=>\t$dest\n";
   
#        rename $src, $dest
#            or warn "Failed to rename $src => $dest: $!";
}

my problem, where I am stuck for the moment, is the test, whether the number
part of my original file name is of a certain amount :

next unless (($nr =~ /(d+)/) and ($1 > 12)) ;

better would be a test, whether $1 is between 1-12 or 13-24 or 25-36 ...

$src            =>          $dest

files1-12       =>          file01_
files13-24      =>          file02_
files25-36      =>          file03_

but there are files with extensions like 01a, 01b, 01c etc which count only
for one !

Could somebody be so kind and give my some help ?

Thank you

marek

Signature

______________________________________________________________________
___PODIUM_INTERNATIONAL_//_the_embassy_for_talented_young_musicians___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
______________________________________________________________________

       
___________________________________________________________
How much free photo storage do you get? Store your holiday
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com

John Horner - 11 Jun 2005 23:31 GMT
> better would be a test, whether $1 is between 1-12 or 13-24 or 25-36
> ...
[quoted text clipped - 4 lines]
> files13-24      =>          file02_
> files25-36      =>          file03_

If I understand your numbering scheme, you need something like this:

$newnumber = printf ("%02d",(int((($oldnumber -1)/12)+1)));

though that looks a little complex! It works in this small script:

for(@ARGV){
printf ("$_ => %02d\n",(int((($_ -1)/12)+1)));
}

when called with

perl numberscheme 1 12 13 24 25 26 36 37

it produces this result:

1 => 01
12 => 01
13 => 02
24 => 02
25 => 03
26 => 03
36 => 03
37 => 04

and Perl will even handle "01a" as 1:

perl numberscheme 01a 12 13 24 25 26 36 37
01a => 01
12 => 01
13 => 02
24 => 02
25 => 03
26 => 03
36 => 03
37 => 04

the best way might be to make a subroutine using that calculation.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.