Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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


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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [holli]: nice. i never thought of hosting a script on the site
    [LanX]: It's not a perfect solution, since it means invoking the monastery several times, but IMHO still better than hosting on another server
    [holli]: i'm surprised it even executes. the server sends text/plain AND a content-dispositio n header
    [holli]: this really really should not be executed as Javascript
    erix's head is getting content indisposed
    [holli]: maybe try a GET instead?
    [moritz]: I must say, when I was very active on perlmonks, I got really used to the markup and all the workflows
    [moritz]: and then I didn't post for a while, and then tried to answer a question. It nearly drove me nuts
    [moritz]: when you try to quote part of a question, you have to know/guess/reverse -engineer what kind of markup they used

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (15)
    As of 2017-11-20 19:17 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:













      Results (291 votes). Check out past polls.

      Notices?