http://www.splintered.co.uk/experiments/92/
get perl : http://www.perl.com/download.csp#win32
rename.pl, ActivePerl and windows
description:
not a full experiment, but something of note for those like me starting with perl on windows...
i recently needed to get a simple method for batch-renaming a large number of files, with a flexible way of controlling the new filenames. after a bit of searching, i came across larry wall's rename.pl script:
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = ) unless @ARGV;
for ( @ARGV )
{
$was = $_;
eval $op;
die $@ if $@;
rename ( $was, $_ ) unless $was eq $_;
}
the script allows for the use of regular expressions, or any other bits of perl code, to create filename replacements.
unfortunately, despite a fresh install of ActivePerl, this code didn't seem to do anything. after some lengthy searching, it turns out that windows' command.com doesn't support wildcard expansion, leaving it up to scripts to handle it instead. if you plan on doing a lot of perl scripting on windows, there's a permanent workaround for this in the Perl under Win32 documentation ... but for a quick fix, the script needs to be modified to emulate the wildcard expansion as follows:
use File::DosGlob;
@ARGV = map {
my @g = File::DosGlob::glob($_) if /[*?]/;
@g ? @g : $_;
} @ARGV;
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = ) unless @ARGV;
for ( @ARGV )
{
$was = $_;
eval $op;
die $@ if $@;
rename ( $was, $_ ) unless $was eq $_;
}
i'm a bit surprised that ActiePerl didn't cater for this already in its vanilla install, but no matter ... from the command line, it's now possible to do simple, yet powerful things like
rename.pl "tr/A-Z/a-z/" *
to change all filenames in the current directory to lowercase. in my particular case, i had a huge number of images from my recent visit to prague, all with IMG_XXXX.jpg filenames. using
rename.pl "s/IMG_/prague-june2007_/" *.jpg
i could simply replace the IMG_ part, resulting in files with a far more useful filename of prague-june2007_XXXX.jpg.
oh, one final note on windows-specific quirks of the command line: always use double quotes for your arguments, as single quotes get passed as literal characters to the script.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment