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

Hi, I want to rename *.abc to *.edf given a directory (So, the program should find all the .abc under the directory and rename each of them to .edf) Thanks, S

Replies are listed 'Best First'.
Re: How to do a recursive rename ?
by merlyn (Sage) on Sep 12, 2000 at 04:43 UTC
    Just a slightly different way of writing it:
    use File::Find; finddepth sub { my $old = $_; return unless -f and s/\.abc$/.def/; warn("won't overwrite existing file at $File::Find::name"), return if -e; rename $old, $_ or warn "Cannot rename $old to $_ in $File::Find::di +r: $!"; }, ".";
    This works even if you rename a directory, because the directory gets renamed last.

    -- Randal L. Schwartz, Perl hacker

Re: How to do a recursive rename ?
by stefp (Vicar) on Sep 12, 2000 at 03:52 UTC
    you loop on the files matched by the glob.
    So for each filename the s/// calculates the new filename $new
    for ( glob "*.abc" ) { ($new = $_) =~ s/\.abc$/.edf/; rename $_, $new or warn "can't rename '$_' to '$new': !"; }
    Thnaks people to indicate me I could edit an already sent nodelet.

    Note 1: On Unix at least, rename will never delete a file. But renaming a file to a clear screen string can be almost as bad.

    Note 2: The camel3 p 154 explains better than I could the idiom ($a = $b) =~ s///

      Use this with caution. rename will destroy any file that gets in its way. Also, you don't need that =~ in front of the s///:
      for ( glob "*.abc" ) { my $old = $_; s/\.abc$/.edf/; rename $old, $_ or warn "can't rename '$old' to '$_'"; }
      Update:
      Ok, I looked again at stefp's code and I see why that =~ was there. If you use that method, don't forget to declare $new somewhere... (you are using strict, aren't you?) and of course, you might want to add some code to make sure rename isn't going to delete anything. maybe:
      for( glob "*.abc" ) { my $new = $_; my $ok = 1; substr $new, 0, -3, "edf"; if( -e $new ) { warn "file '$new' already exists. Do you want to replace?\n"; while(<STDIN>) { last if /[yY][eEsS]{0,1}/; $ok = 0, last if /[nN][oO]{0,1}/; warn "Yes or No?\r"; } } rename $_, $new or warn "can't rename '$_' to '$new'\n " if $ok; }
      That is untested...
Re: How to do a recursive rename ?
by Anonymous Monk on Sep 12, 2000 at 04:07 UTC
    Here is a novice's way:
    #!/usr/bin/perl -w use strict; use File::Find; my $dir = '.'; # or wherever find(\&myrename, $dir); sub myrename { my $file1 = $_; my $file2 = $_; if ($file2 =~ s/\.abc$/.edf/) { print "renamed $file1, $file2\n" if rename $file1, $file2; } }
    rudif
      I hope there are no directories named "*.abc". Here's another way:
      #!/usr/bin/perl -w use strict; use File::Find; my $dir = '.'; # or wherever find(\&myrename, $dir); sub myrename { return unless -f and /(.*?)\.abc$/s; my $new_name = $1.".edf"; if (rename $_, $new_name) { print "renamed $File::Find::name to $new_name\n"; } else { warn "Couldn't rename $File::Find::name to $new_name: $!"; } }
Re: How to do a recursive rename ?
by stefp (Vicar) on Sep 12, 2000 at 03:55 UTC
    BTW there is no recursion here just iteration.
    probably you are human not divine. :)
Re: How to do a recursive rename ?
by stefp (Vicar) on Sep 12, 2000 at 04:05 UTC
    you loop on the files matched by the glob.
    So for each filename the s/// calculates the new filename $new
    for ( glob "*.abc" ) { ($new = $_) =~ s/\.abc$/.edf/; rename $_, $new or warn "can't rename '$old' to '$new'"; }
      Oh, I see what you're doing now. I kind of glanced at it and said, "but why would he bind $_ to a pattern match?"... he he, because you aren't.

      By the way, you can edit your old posts to add updates and the like (or to fix mistakes like your $old) by going to the node itself.

Re: How to do a recursive rename ?
by stefp (Vicar) on Sep 12, 2000 at 04:05 UTC
    Oops. I meant $_ instead of $old. The poor froggy I am is not yet used to write English, Perl and html at once . I am not merlyn yet :(