Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How do I do a bulk rename?

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


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: *.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"; } } }
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (2)
    As of 2021-03-01 05:14 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found