Renames files in a directory. If no directory is specified, the current directory is used.

Source Code : 
#! /usr/bin/perl

##
## Renames files in a directory. If no directory is
## specified, the current directory is used.
##

use strict;
use 
warnings;
use 
File::Copy;
use 
Getopt::Long;

my $directory "";
my $from      "";
my $to        "";

my $getOptionsResult=GetOptions(
    
"--directory=s" => \$directory,
    
"--from=s"      => \$from,
    
"--to=s"        => \$to,
    );


&
displayUsageAndDie if $to eq "";
&
displayUsageAndDie if $from eq "";
$directory "." if $directory eq "";


opendir(my $dh,$directory) or die "Unable to open $directory: $!";
while(
readdir($dh))
{
    &
renameFiles($_,$to) if m/$from/;
}
close($dh);


sub renameFiles()
{
    
my $f $_;
    
chomp $f;
    (
my $t $f) =~ s/$from/$to/g;
    
printf("renaming '%s' to '%s'\n",$f,$t);

[Login or Register to remove this advertisement]

    
move($f$t) or die(qq{failed to move $f -> $t});
}


sub displayUsageAndDie()
{
    print 
"
Renames files in a directory. If no directory is
specified, the current directory is used.

usage:
    rename --from from --to to [--directory directory]

"
;
    exit 
1;

Mobile_Guru Reviewed by Mobile_Guru on . Rename files using perl Renames files in a directory. If no directory is specified, the current directory is used. #! /usr/bin/perl ## ## Renames files in a directory. If no directory is ## specified, the current directory is used. ## use strict; Rating: 5