Rename a group of files with a common extension? : rename « File « Perl






Rename a group of files with a common extension?

   

    #!/usr/local/bin/perl -w

    use Getopt::Long;
    use File::Basename;

    my $ret = GetOptions ("e|extension:s");
    my $ext = $opt_e || die "Usage: $0 -e Extension\n";
    my $filename;

    opendir (DIR, ".") || die "Can't open directory . $! \n";
    my @filelist = grep (/$ext$/, readdir (DIR));
    closedir (@filelist);

    for $filename (@filelist)
    {
       my $base = basename($filename, $ext);
       print "Renaming $filename -> $base\n";
       if (!rename $filename, $base)
       {
          print "Could not rename file $filename : $!\n";
       }
    }

   
    
    
  








Related examples in the same category

1.Rename a file
2.Renaming Files: rename(OLDFILENAME, NEWFILENAME);
3.Renaming a file before accidental deletion