Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

use perl; | 21 comments | Create New Account
Click here to return to the 'use perl;' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
use perl;
Authored by: babbage on May 09, '01 03:21:16PM
Care for a perl solution? Save the following as something like ~/bin/rename:
#!/usr/bin/perl -w
# rename - larry wall's filename fixer

$op = shift or die "Usage: rename expr [files]n";
chomp (@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
   $was = $_;
   eval $op;
   die $@ if $@;
   rename ($was,$_) unless $was eq $_;

Now if you're comfortable with regular expression syntax (and hey, who isn't? ;), all you have to type is the following:

rename 's#.jpg#.jpg.bak#' *jpg

Note that this command doesn't come with an "undo" button, and can make a mess if you're not careful. As I am often not. :) If you *are* careful however, this can be a nice shortcut...

[ Reply to This | # ]

use perl;
Authored by: robh on May 10, '01 05:05:11AM

I wrote a similar Perl script for myself. Unlike Larry's, mine shows a list of changes that are to be made and prompts the users to confirm it's okay to proceed. When it comes to renaming files with Perl expressions, it's vital to see if your expression does what you think it does before it does it :-) (I often have to adjust the expression until I get it just right).

Here's the script... #!/usr/bin/perl $expr = $ARGV[0]; @files = @ARGV[1..$#ARGV]; foreach (@files) {  $old = $_;  eval($expr);  next if $_ eq $old;  print "$old ==> $_n";  $RENAME{$old} = $_; } $|=1; print "Rename these ? "; if ( <STDIN> =~ /y/i) {  foreach $f (keys %RENAME) {   print STDERR "mv $f $RENAME{$f}n";  print STDERR `mv '$f' '$RENAME{$f}'`;  } } else {  print STDERR "Nothing renamedn"; } I also named the script "rename" and run it like this example: rename 's/.jpg/.jpeg/' *.jpg The output showing what will happen looks like this example: Fire_and_deer.jpg ==> Fire_and_deer.jpeg crash.jpg ==> crash.jpeg floods.jpg ==> floods.jpeg myrrh.jpg ==> myrrh.jpeg Rename these ?

[ Reply to This | # ]
Learning Perl
Authored by: Anonymous on May 10, '01 06:39:11AM

Of course the point of the advanced rename (thank you) is that it lets you check before committing, but just to be perlsnickety, with 's/.jpeg/.jpg/' you would get:

hajjpeg ==> hajjpg

(because the . matches anything; use a backslash before the . to match just a period.)

[ Reply to This | # ]
Learning Perl
Authored by: robh on May 10, '01 07:09:55AM

You're right. There are various workarounds, if I was worried about mismatches I'd use 's/\.jpg$/.jpeg/'

I avoided the '\'; in my earlier example because the forum software here keeps eating escape characters after doing a preview of the comment. I hope they're intact here :-)

[ Reply to This | # ]
use perl;
Authored by: Anonymous on May 14, '01 04:58:47PM

this doesn't seem to work right on files with spaces in their names, whereas Larry's did.

[ Reply to This | # ]
use perl; correction
Authored by: Anonymous on May 14, '01 05:01:04PM

sorry, it turns out only the feedback message is broken for files with spaces in their names.
the script renames them just fine.

[ Reply to This | # ]
use a GUI file renamer?
Authored by: osxpounder on Mar 20, '03 04:47:11PM

I apologize in advance if I misunderstood the point, but it seems to me that if ease and simplicity are valuable to you, you might want to try FileRenamer.

I checked MacUpdate a minute ago, and it was still available. Apparently the authors' site is down, and it seems not to have been finished, but it works... and I use it. It lets you select a folder with files to be renamed [and that's 1 difference I see now between this and your CLI suggestions], offers some useful options such as appending or prepending sequential numbers, and it previews changes before committing. I use it to rename 100s of TGA files rendered from a 3D app so that they will sequence properly when I load them into QuickTime.

Hope this helps,



[ Reply to This | # ]