Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: renaming files

by fundflow (Chaplain)
on Jan 04, 2001 at 00:48 UTC ( [id://49621]=note: print w/replies, xml ) Need Help??


in reply to renaming files

You want to do something like:
for (<*>) { if(/\.TMB/i) { $old=$_; s/\.TMB$/T\.JPG/; die "$_ already exists" if (-e $_); rename($old, $_) or die "Failed to rename\n"; } }
update: added a check for exising file, following lemming's suggestion

Replies are listed 'Best First'.
Re: Only Works in UPPER CASE
by purp (Novice) on Jan 04, 2001 at 02:44 UTC

    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";
    
      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)
Re: Re: renaming files
by salvadors (Pilgrim) on Jan 04, 2001 at 16:57 UTC
    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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://49621]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-24 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found