Renaming File Extensions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bdan44
    New Member
    • Apr 2007
    • 11

    Renaming File Extensions

    i am a total n00b at perl. i just started learning yesterday. I wanted to make a script to rename extentions. this is what i've come up with. i can't figure out why it doesn't work. any help would be appreciated.

    Code:
    #!/usr/bin/perl
    
    print "What ending would you like to change?";
    $ending = <STDIN>;
    chomp($ending);
    
    print "What would you like to change it to?";
    $new_ending = <STDIN>;
    chomp($new_ending);
    
    print "What is the directory you would like to change this in?";
    $directory = <STDIN>;
    chomp($directory);
    
    print $ending, " ", $new_ending, " ", $directory, "\n";
    
    chdir $directory;
    @list = `ls *.$ending`;
    
    foreach (@list) {
    	chop;
    	rename (".$ending", ".$new_ending") || die "Cannot delete $ending";
    };
    print "\n";
    Last edited by miller; Apr 10 '07, 08:57 PM. Reason: Code Tag and ReFormatting
  • savanm
    New Member
    • Oct 2006
    • 85

    #2
    Hi,

    Code:
    use Cwd;
    
    $path=getcwd();
    $path=~s/\\/\//sg;
    print $path;
    
    opendir(DIR,$path) || die("Cannot open thedirectry");
    @storage = grep(/\.xml/,readdir(DIR));
    close(DIR);
    
    foreach $fil(@storage) {
    	submain($path."\/".$fil);
    }
    
    sub submain() {
    	$xml = shift;
    	$txt = $xml;
    	$txt=~s/\.xml/\.txt/sgi;
    	local $/;
    	open(FILE,$xml);
    	while(<FILE>) {
    		$temp=$_;
    	}
    	open(OUT,">$txt");
    	print OUT $temp;
    	close(OUT);
    
    }

    Try this this is used to change the extension of the file .xml to .txt

    then tell ur need briefly
    Last edited by miller; Apr 10 '07, 08:58 PM. Reason: Code Tag and ReFormatting

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      You need the full filename:

      Code:
      rename ("fullname.$ending", "fullname.$new_ending") || die "Cannot delete $ending";

      also, probably safer to use "chomp" instead of "chop" athough it may not matter in this case. But chomp is really the correct function for removing the record seperator, chop blindy removes the last charater on the end of the string regardless of what the character is.

      Comment

      • bdan44
        New Member
        • Apr 2007
        • 11

        #4
        hi,
        what I want the code to do is prompt the user for the extensions they would like to change (if they wanted to change from .mpg to .mpeg they could or from .jpg to .jpeg they could, all with the same script).

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Hi bdan44,

          As Kevin pointed out, you need to give an explicit file name to the rename function, not just the extension.

          Also, your rename won't work as you desire unless you extract the non extention part of the file. You can use either a regex, substr, or a core library for that. I'd suggest a core library.

          Here is your code fixed and cleaned up by adding "use strict;".

          Code:
          #!/usr/bin/perl
          
          use File::Basename qw(fileparse);
          
          use strict;
          
          print "What ending would you like to change?";
          my $old_ending = <STDIN>;
          chomp($old_ending);
          
          print "What would you like to change it to?";
          my $new_ending = <STDIN>;
          chomp($new_ending);
          
          print "What is the directory you would like to change this in?";
          my $directory = <STDIN>;
          chomp($directory);
          
          print $ending, " ", $new_ending, " ", $directory, "\n";
          
          chdir $directory;
          my @list = `ls *.$ending`;
          
          foreach (@list) {
          	chomp;
          	my $name = fileparse($_, ".$old_ending");
          	rename("$name.$old_ending", "$name.$new_ending") or die "Cannot delete $ending";
          };
          print "\n";
          (Not tested)

          - Miller

          Comment

          Working...