Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

rename file extension

by muizelaar (Sexton)
on Feb 24, 2010 at 15:37 UTC ( [id://825091]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks Its been a while since i was on here but could do with some help. I would like to rename the file extensions of all the files listed in an array from ".tst" to ".test" I would also like to send the output to a file. I have done this before but this time I can’t see what I’m doing wrong. Please help! :-) I get the following error - Can't modify constant item in list assignment at testscript line 23, near ");" Execution of ediorder aborted due to compilation errors (#1)
my %ext_map = ( '.tst' => '.test' ); open(FHA, ">renamebatch"); foreach my $file (@orderlist) { (old_filename, undef, $ext) = fileparse($file,qr{\..*}); my $new_ext = $ext_map{$ext} || $ext; my $new_filename = $old_filename; print(FHA "$new_filename$new_ext"); }

Replies are listed 'Best First'.
Re: rename file extension
by kennethk (Abbot) on Feb 24, 2010 at 15:48 UTC
    You have forgotten your sigil on old_filename. I think the line should probably read

    my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});

    I presume that would be line 23? Note that this would have been caught by strict and the resulting errors would have made the omission quite obvious.

      Hi thanks for that but I now seem to seeing this other problem again something I have seen before but I guess this is what happens when you have spent almost a year not doing anything with Perl. Help much needed :-) I now see the following error Undefined subroutine &main::fileparse called at testedi line 17 (#1) (F) The subroutine indicated hasn't been defined, or if it was, it has since been undefined. Uncaught exception from user code: Undefined subroutine &main::fileparse called at testedi line 17. I have checked and double checked for mistakes like the one you pointed out, I’m sure it’s going to be something really silly again. Help much needed :-)
      #!/usr/bin/perl -w use diagnostics; use Cwd; my $dir = getcwd; my $edihome = "/home/dsadmin/EDI"; opendir(FH, $edihome); @orderlist = grep { !/^\.\.?\z/ && -e "$edihome/$_" } readdir FH; @orderlist = glob "*.tst"; my %ext_map = ( '.tst' => '.test' ); open(FHA, ">renamebatch"); for my $file (@orderlist) { my ($old_filename, undef, $ext) = fileparse($file,qr{\..*}); my $new_ext = $ext_map{$ext} || $ext; my $new_filename = $old_filename; print(FHA "$file\n"); }
        Add this:
        use File::Basename;
        And, in the future, wrap your warning/error messages in code tags as well.
Re: rename file extension
by Ratazong (Monsignor) on Feb 24, 2010 at 15:49 UTC
    (old_filename, undef, $ext) = ...

    I suppose there is a $ missing in front of old_filename

Re: rename file extension
by chuckbutler (Monsignor) on Feb 24, 2010 at 16:33 UTC

    One thing I would change after correcting for the afore mentioned syntax error would be

    ... print(FHA "$new_filenname$new_ext\t$file\n"); ...
    if you need to change one file name to another. Depending on what you need to do with the output, this will let you audit the change and only act on items that need a change. Good luck. -c

Re: rename file extension
by nikosv (Deacon) on Feb 24, 2010 at 16:43 UTC
    Let's say orderlist contains the following:
    @orderlist=qw(1.tst 2 3 4.tst); @orderlist=grep {s#\.tst$#\.test#} @orderlist; print @orderlist; #prints 1.test 4.test

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found