|
|
use perl;
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...
use perl;
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 ?
Learning Perl
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:
Learning Perl
You're right. There are various workarounds, if I was worried about mismatches I'd use 's/\.jpg$/.jpeg/'
use perl;
this doesn't seem to work right on files with spaces in their names, whereas Larry's did.
use perl; correction
sorry, it turns out only the feedback message is broken for files with spaces in their names.
use a GUI file renamer?
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. |
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.09 seconds |
|