Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How do I do a bulk rename?

( #19264=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 21, 2000 at 20:15 UTC
Q&A  > files


Description:

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

Answer: How do I do a bulk rename?
contributed by jjhorner

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
Answer: How do I do a bulk rename?
contributed by Shendal

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
Answer: How do I do a bulk rename?
contributed by kryberg

#!/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
Answer: How do I do a bulk rename?
contributed by fundflow

You can use Rename files safely which will make sure that no files are erased in the process.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (8)
    As of 2014-10-01 00:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (386 votes), past polls