Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Changing filename extentions

by Anonymous Monk
on Jul 24, 2002 at 17:20 UTC ( [id://184945]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Trying to rename all uppercase HTML and HTM extentions to lowercase. Such as:
filename.HTML should be renamed to filename.html

Want this done to all files in my directory.
Not sure if my system renaming part on my Windows NT will work and the lc function?
opendir(DIR,$file); @files = readdir(DIR); close(DIR); for (@files){ if($file =~ /\.HTML/ || /\.HTM/) { system("rename $file (lc)$file"); } }

Replies are listed 'Best First'.
Re: Changing filename extentions
by IlyaM (Parson) on Jul 24, 2002 at 17:31 UTC
    Just use File::Copy:

    use File::Copy; opendir(DIR,$file); my @files = readdir(DIR); close(DIR); for my $file (@files) { if($file =~ /\.HTML?$/) { my $new_file = lc $file; move($file, $new_file) or die "Can't move file: $!"; } }

    Update: Fixed incorrect Perl construct. Thanks to gav^ for noticing it.

    Ilya Martynov (

Re: Changing filename extentions
by gav^ (Curate) on Jul 24, 2002 at 17:34 UTC
    Hmm. Your code isn't even remotely valid perl. Perhaps you should look at the documentation a bit more?

    Here is something that would work

    opendir(DIR, $dir) or die "Cannot open directory '$dir': $!"; my @files = readdir(DIR); close(DIR); foreach my $file (@files) { if ($file =~ /\.HTML?$/i) { system("rename $file \L$file\E"); } }


Re: Changing filename extentions
by DamnDirtyApe (Curate) on Jul 24, 2002 at 17:35 UTC

    A couple of things here:

    1. $file is not the current item in the @files array.
    2. The function call will not work inside the quotes.

    This is untested, but I'd go with something more like this:

    my $file = '.' ; opendir( DIR, $file ) ; @files = readdir( DIR ) ; close( DIR ) ; for ( @files ) { if ( /\.HTM[L]?/ ) { system("rename", $_, lc( $_ ) ); } }

    Or, even better (if it's supported on NT), just use Perl's rename command.

    rename $_, lc( $_ ) ;

    D a m n D i r t y A p e
    Home Node | Email
Re: Changing filename extentions
by Fastolfe (Vicar) on Jul 24, 2002 at 19:59 UTC
    Since you're renaming in the same directory, using File::Copy is overkill. The use of system is inefficient when Perl has a rename function built in, and using readdir for this purpose is probably unnecessary.
    rename($_, lc($_)) foreach <*.HTM{,L}>; # yes, this is portable
    I think it was Larry Wall that pushed a real simple command-line regex-based "rename" utility like this:
    $op = shift or die "Usage: rename expr [files]\n"; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die if $@; rename($was, $_) unless $was eq $_; }
    Which would let you run something like:
    rename '$_ = lc' *.HTM *.HTML

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://184945]
Approved by kodo
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-20 19:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found