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]

Reply via email to