• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 749
  • Last Modified:

perl script to capitalize file extension

What is wrong with the perl program I just wrote?

I am trying to make a perl script called "upcasejpg".  I want to write:
    upcasejpg somedirectory
I want it to look in the directory and if it sees a program with the extension "jpg", it will capitalize the extension to "JPG".  For example, assume that there is a picture called "a_pic.jpg" in the directory "a_directory".  I would like to type the following:

    upcasejpg a_directory

the following will happen:
    before                                           after
    ----------                                         --------
    ./a_directory/a_pic.jpg                     /a_directory/a_pic.JPG

Here is my script that doesn't work:
==========================
#!/usr/bin/perl -w
#

$arg = @ARGV;
if ( $arg < 1 ) {
 print "not enough arguments\n";
 die;
}

opendir ( DIR, $ARGV[0] ) or die "cannot open directory";
print "\nopening: $ARGV[0]\n";

while ( $name = readdir(DIR) ) {
 if ( $name =~ /\.jpg/ ){
   print "\ngoing to capitalize: $name\n";
   $newname = $`.".JPG";
   print "\nnew name: $newname\n";
   rename ( $name, $newname );
 }
}

========================

What is wrong with it?  I type what I told you.  It says that it is capitalizing the extension.  When I look in the directory after running the script, it has done nothing.

 ?!?!?!?!?
closedir ( DIR );
0
dwcronin
Asked:
dwcronin
  • 3
  • 2
1 Solution
 
mjcoyneCommented:
Try it this way:

#!/usr/bin/perl -w
use strict;

my $arg = @ARGV;
if ( $arg < 1 ) {
 print "not enough arguments\n";
 die;
}

opendir ( DIR, $ARGV[0] ) or die "cannot open directory";
print "\nopening: $ARGV[0]\n";

while (my $name = readdir(DIR) ) {
 if ( $name =~ /\.jpg/ ){
   print "\ngoing to capitalize: $name\n";
   my $newname = $name;
   $newname =~ s/\.jpg/\.JPG/;
   print "\nnew name: $newname\n";
   rename ( $name, $newname );
 }
}
0
 
mjcoyneCommented:
Actually, I think you're going to have to chdir() into the directory you're looking in before you call rename for this to work, or provide a full path name plus the old and new filenamws to rename.

What operating system are you on?
0
 
ozoCommented:
rename ( "$ARGV[0]/$name", "$ARGV[0]/$newname") or warn "$ARGV[0]/$name, $ARGV[0]/$newname $!";
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TintinCommented:
This is how I would write it:

#!/usr/bin/perl
$dir = shift or die "Usage: $0 <dir>\n";
die "$dir does not exist\n" unless -d $dir;

foreach $file (<*.jpg>) {
   ($newfile = $file) =~ s/\.jpg/\.JPG/;
   print "Renaming $file to $newfile\n";
   rename $file,$newfile or warn "Can not rename $file to $newfile $!\n";
}
0
 
mjcoyneCommented:
A better way to write it, I agree, but you're still going to have to move inro the jpg directory or pass the path to rename...
0
 
TintinCommented:
Whoops,

foreach $file (<*.jpg>) {

should have been

foreach $file (<$dir/*.jpg>) {
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now