Unsure, but here is at least a shot using a startat and changeto:
#!perl -w
my $MyStartAt ;
my $MyChangeTo ;
my $MyNewName  ;

while ( <DATA> ) {
   chomp;
   next if ( /^\s*$/ );
   if ( /^startat:\s+(\d+)/i ) {
      $MyStartAt = $1;
      next;
    }elsif ( /^changeto:\s+(\w+)/i ) {
      $MyChangeTo = $1;
      next;
    }
   if ( $MyStartAt eq '' or $MyChangeTo eq '' ) {
      printf "Missing either the Start At Sequence or the Change To.\n";
      printf "Start At  seq: <$MyStartAt>\n";
      printf "Change To seq: <$MyChangeTo>\n";
      printf "correct and rerun.\n";
      last;
    }
   my $MyOld = $_;
   if ( /(\w+)(\d+)\.jpg/i ) {
      my $MyFirst = $1;
      my $MyDigits = $2;
      $MyNewName = sprintf "%-s%-s.jpg", $MyChangeTo,($MyDigits+$MyStartAt);      
    }
   printf "Rename $MyOld, $MyNewName\n";
 }

__DATA__
startat: 100
Changeto: foo

image1.jpg
image7.jpg
image10.jpg
^---- Script ends here
Output:
Rename image1.jpg, foo101.jpg
Rename image7.jpg, foo107.jpg
Rename image10.jpg, foo100.jpg

Wags ;) 


-----Original Message-----
From: Booher Timothy B 1stLt AFRL/MNAC
[mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 11, 2001 09:31 
To: [EMAIL PROTECTED]
Subject: RE: Use perl to change File names


Over the last evening I have reviewed these scripts and understand what is
going on, but they still don't quite solve my problem. I want to fix the
following situation.

Image4.jpg, image5.jpg, image6.jpg - to --> foo78.jpg, foo79.jpg, foo80, etc
... .. 

This means I want to change the number not just the word involved. So for
example I would want to give the parameter StartAt: '78' and 's/image/foo' -
say I wanted to start at any arbitrary number given another arbitrary
number, by specifing the offset (here 74). This is what I am having trouble
doing . . . I have a feeling that it is possible . . . or did the original
solution peter gave me provide a slight modification . . . ?

Thanks for any help . . .

tim

____________________________________________________
Timothy B Booher, Lt USAF, AFRL/MNAC
101 West Eglin Blvd, Suite 339 
Eglin AFB FL 32542-6810 
Phone: 850-882-8302 Ext. 3360 (DSN 872-)
FAX: 850-882-2201

 -----Original Message-----
From:   Peter Scott [mailto:[EMAIL PROTECTED]] 
Sent:   Monday, December 10, 2001 12:09 PM
To:     Booher Timothy B 1stLt AFRL/MNAC; [EMAIL PROTECTED]
Subject:        Re: Use perl to change File names

At 11:28 AM 12/10/01 -0600, Booher Timothy B 1stLt AFRL/MNAC wrote:
>I want to be able to rename a file name from one number series to
>another number series ie:
>
>Image1.jpg
>Image2.jpg
>Image3.jpg
>....
>
>to
>
>Foo234.jpg
>Foo235.jpg
>Foo236.jpg
>....
>
>Is there a way to do this using regular expressions? Is there a more
elegant
>way to adding a counter to the script above and hard-code the argument?

Quick-n-dirty:

for (glob("Image*.jpg")) {
   /(\d+)\./ and rename $_, "Foo$1.jpg";
}

A bit more flexible:

# my_rename("Image", "Foo");
sub my_rename {
   my ($from, $to) = @_;
   for (glob("$from*.jpg)) {
     /(\d+)\./ and rename $_, "$to$1.jpg";
   }
}

Even more so:

# my_rename('Image(\d+).jpg', 'Foo$1.jpg');
sub my_rename {
   my $from = qr/^$_[0]$/s;
   for (glob "*") {
     next unless /$from/;
     my $to = eval qq("$_[1]");
     rename $_, $to unless -e $to;
   }
}

Watch that the backslashes get passed through properly, and be aware that 
glob() can have trouble with large result sets pre-5.6.

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to