http://www.perlmonks.org?node_id=19264

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

I want to move xxx_yyy.zzz to xxx-yyy.zzz, but cant figure out how to automate this. Any ideas?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I do a bulk rename?
by jjhorner (Hermit) on Jun 21, 2000 at 21:26 UTC

    Try this:

    #!/usr/bin/perl -w foreach (@ARGV){ $name = $_ ; $name =~ s/_/-/; rename($_, $name); print "$_ renamed to $name\n"; } usage: renamer.pl *.zzz
    J. J. Horner
    
Re: How do I do a bulk rename?
by Shendal (Hermit) on Jun 21, 2000 at 20:33 UTC
    Use File::Copy.

    use File::Copy; use strict; my($sourcedir) = shift || die "No source dir"; opendir(DIR,"$sourcedir"); foreach (grep !/^\.\.?$/,readdir DIR) { if (/^(\S+)_(\S+)\.(\S+)$/) { if (move($_,"$1-$2.$3")) { print "Moved $_ to $1-$2.$3\n"; } else { print "Error moving $_ to $1-$2.$3\n"; } } }
    HTH,
    Shendal
Re: How do I do a bulk rename?
by kryberg (Pilgrim) on Oct 23, 2002 at 15:10 UTC
    #!/usr/bin/perl -w # rename all files with a certain extention in a directory use strict; my $dirname; $dirname = '/substitue_your_path_here'; opendir(DIR, $dirname) or die "Can't opendir $dirname: $!"; while ( defined (my $file = readdir DIR) ) { # ignore current and parent directory . and .. next if $file =~ /^\.\.?$/; my $new = $file; # substitute new file extension $new =~ s/\.old_extension$/\.new_extension/; rename($file,$new) }
    Reference Perl Cookbook Chapter 9 and Programming Perl Chapter 29
Re: How do I do a bulk rename?
by fundflow (Chaplain) on Oct 23, 2002 at 16:13 UTC