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

Hi monastery dwellers

I have been trying to figure out a way to do this but have not figured it out. I want to change all files with the extension 'abc' to the extension 'def',
I have tried using
@filelist = glob(*.abc");
as a starting point but can't quite get it to work. I tried using fileparse() also but no luck
Thanks

mndoci

"What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

Replies are listed 'Best First'.
Re: Help on renaming a bunch of files
by chipmunk (Parson) on Jul 11, 2001 at 06:11 UTC
    I've found the following script very useful for this task. This is the 'rename' script from Programming Perl, 1st Edition, updated a bit for perl5.
    #!/usr/local/bin/perl # Usage: rename <perlexpr> [files] ($op = shift) || die "Usage: $0 <perlexpr> [files]\n"; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; }
    The book also includes some examples of using the script.

    Rename files matching *.bak to strip the extension:   rename 's/\.bak$//' *.abc Add the extensions back on:

    rename '$_ .= ".bak"' * rename 's/$/.bak/' *
    Translate uppercase names to lower:   rename 'tr/A-Z/a-z/' * And more:
    rename 's/foo/bar; $_ = $was if -e' *foo* find . -print | rename 's/readme/README/i' find . -print | rename 's/$/.old/ if -M $_ > 0.5' find . -name '*,v' -print | \ rename 's#(.*)/#$1/RCS#, $x{$1}++ || mkdir("$1/RCS", 0777)'
    It was sad that the later editions of Programming Perl didn't include the sample scripts, but fortunately the Perl CookBook has since filled that need.
Re: Help on renaming a bunch of files
by blakem (Monsignor) on Jul 11, 2001 at 04:20 UTC
    Using super search I was able to find an in depth discussion at this node quite quickly.... perhaps the answer you are looking for is there.

    -Blake

      Thanks a ton
      I did not know about this node.

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'
Re: Help on renaming a bunch of files
by tachyon (Chancellor) on Jul 11, 2001 at 04:31 UTC

    Try this, it uses a glob to read in a list of files that match the desired directory and name structure, generates the new name from the old one, and then renames them. In this case we rename all the files that end with 'pl' so that they instead end with 'bak';

    my $dir = "c:/"; while (<$dir*.pl>) { my ($old,$new); $old = $new = $_; $new =~ s/pl$/bak/; rename $old, $new; }

    Hope this helps

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Worked like a charm

      Thanks a ton

      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'
Re: Help on renaming a bunch of files
by rrwo (Friar) on Jul 11, 2001 at 04:15 UTC

    Use grep:

    foreach my $file (grep /\.abc$/, @file_list) # @file_list is the list of files, such as from # reading a dirhandle { # call to sub that renames file here }