in reply to Re: renaming files
in thread renaming files

If you change:

    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";

Replies are listed 'Best First'.
Re: Re: Only Works in UPPER CASE
by fundflow (Chaplain) on Jan 04, 2001 at 03:28 UTC
    Corret about the /i thing.

    Also, instead of filtering out using grep, you can just glob the files that you are looking for by:

    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)