Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do I do a bulk rename?

by Anonymous Monk
on Jun 21, 2000 at 20:15 UTC ( #19264=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-04-20 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?