s/\.TMB$/T\.JPG/;
...to:
s/\.TMB/T.JPG/i;
...you'll catch all cases of all cases. ;]
Meanwhile, here's a total treatment with a bit of commentary; it uses some neat grep tricks a friend taught me a while back to narrow the list of files before you start processing. I've only tested it a little but it should be correct.
Cheers!
--j
#!/usr/bin/env perl
#
# mvThumbs:
# A quick script to move thumbnails
#
use strict;
use Cwd;
if (! @ARGV) {
# We need arguments, durnit!
warn "${0}: Insufficient arguments\n";
warn "Syntax: $0 <dirName> ...\n";
exit(1);
}
# They may give us directory paths which are relative to where we are,
# so let's be sure of where we are, eh?
my $origCWD = cwd();
foreach my $dirName (@ARGV) {
unless (-d $dirName) {
warn ">>> $dirName is not a directory ... skipping.\n";
} else {
# Can we write here? If not, we may as well skip it
unless (-w $dirName) {
warn ">>>> No write permissions in $dirName ... skipping\n";
} else {
# Get the list of thumbnails in this directory
opendir(thumbDir, $dirName);
my @thumbList = grep /\.TMB$/i, readdir(thumbDir);
closedir(thumbDir);
# To be lazy about having to construct pathnames, we'll just
# move to the directory we're working with.
chdir($dirName);
print "\nIn directory: $dirName\n";
# Run through the list and rename 'em
foreach my $thumbFile (@thumbList) {
my $newFile = $thumbFile;
$newFile =~ s/\.TMB$/T.JPG/i;
# You could force the filename into lower or upper case here
# $newFile = lc($newFile);
# $newFile = uc($newFile);
unless (-e $newFile) {
printf("%-30s => %s\n",$thumbFile, $newFile);
rename($thumbFile, $newFile);
} else {
warn ">>> Can't move $thumbFile => $newFile in $dirName;\n";
warn ">>> $newFile already exists. Skipping.\n";
}
}
# Back to home base for the next one
chdir($origCWD);
}
}
}
print "\nAll done!\n";
| [reply] |
foreach my $thumbFile (glob('*.TMB')) {...}
(I left the core like the original question, just to make
it easy for the guy/gal in the original post) | [reply] [d/l] |
Hmmm ... surely you don't actually want to 'die' in the middle of a for loop just because one of your files already happens to have the new extension?
i.e., if I had apple.TMB and apple.JPG then none of banana.TMB, pear.TMB or wombat.TMB would get renamed...
Tony | [reply] |
1. As the (updated) song sais: "This is my script and i'll die if i want to" :)
2. There wouldn't be a problem with your example as apple.tmp goes to applet.jpg
Anyway, just as you noticed it, I'm sure that Anonymous Monk that
posted the original question will be able to adopt the script
to his needs. He didn't specify what to do in such cases so
better be safe than sorry.
| [reply] |