Change File Extensions

under Programming Perl Gist

 

This script will append an extension to the end of the given files, optionally removing any existing extensions. Don’t ask me why I wrote this, must have been important at one point in time. Alot easier to do this via find in un*x. Giving the following files in a directory:

.                   file2.log
..                  file3.errors
file1.doc           file4.log

Running chext -d txt *.log the follow is the results to the above files:

.                   file2.txt
..                  file3.errors
file1.doc           file4.txt

 

#!/usr/local/bin/perl
# Copyright (C) 1998 Andrew Loree
# $Id: chext.pl,v 1.1.1.1 2003/10/05 21:51:29 andy Exp $
############################################################################
#
# chext - Change file extenstions to 'ext', appending them to the end of the
#         file. Optionally, all other extension can be dropped.  Type 
#         'chext' at the shell prompt for specific usage
#
############################################################################
$version = "1.0";

if ($#ARGV < 1){   # Check arguments
  print <<END_of_usage;
usage: chext [-d] \'ext\' files
version $version
Changes file extension to \'ext\', by appending to the end of the file.  The \'ext\' should not contain a \'.\'

  -d    Optionally, drop all other extensions, and then replace them with
        \'ext\'

Copyright (c) 1998, Andrew Loree
END_of_usage

  exit(-1);
}

if ($ARGV[0] eq "-d"){
  $opt_d = 1;
  shift(@ARGV);    
}


$ext = $ARGV[0];     # Process the extension
if ( $ext !~ /^\./ ){
  $ext = ".$ext";    # Add . at the begining if needed
}

# Remove the first ARGV, it's the ext
shift(@ARGV);

foreach $old_filename (@ARGV){                     # Process the files
  if (-f $old_filename){
    if ($opt_d){                                   # Drop all other extensions
      $new_filename = $old_filename;
      if ( $old_filename !~ /\./ ){                    
    $new_filename = $old_filename . $ext;
      }
      else{
    $new_filename =~ s/\.(.*)/$ext/;
      }  
    }
    else{
      $new_filename = $old_filename . $ext;
    }
    if ($new_filename eq $old_filename){            # Don't bother if same name
      next;
    }
    if (-f $new_filename){                          # New file already exists
      print "The file \'$new_filename\' already exists. Replace it [y|n] ?";
      $answer = <STDIN>;
      if ($answer !~ /^y/i){
    next;                 # Next file
      }
    }
    if (rename($old_filename, $new_filename) == 0){
      print "Unable to rename $old_filename to $new_filename\n";
    }
  }
  else{
    print "Invalid filename: $old_filename\n";
  }
}