Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I was cleaning up my mp3 collection and realized I needed to do some file renaming. Some of it was easy, and some of it was a bit more complicated. I tried the Cookbook Example, but it didn't have a preview option (something I needed because I often get regexes wrong on the first try).

It's useful for me, hopefully someone else will find it handy as well.

Note: This was tested on Windows. I haven't run it on a Unix platform yet.

use strict; &renameFiles( &getArgs() ); # ------------------- SUBROUTINES ------------------- sub printUsage { print <<EOF; Usage: > perl ["<regex>" [<preview>]] Where, * <regex> the regular expression to use * <preview> is 0 to disable preview; anything otherwise EOF } sub getArgs { my ($reg, $prompt) = ("", 1); if ( @ARGV == 2 ) { ($reg, $prompt) = @ARGV; unless ( $prompt == 0 ) { $prompt = 1; } } elsif ( @ARGV == 1 ) { ($reg) = @ARGV; } else { # unexpected number of args &printUsage(); exit; } return ($reg, $prompt); } sub renameFiles { my ($reg, $prompt) = @_; my @files = <*>; foreach (@files) { my $before = $_; eval $reg; my $after = $_; if ( $before eq $after ) { next; } # nothing to do if ( $prompt ) { print "$before => $after\n"; print "Rename? [Yes|No|Always|Cancel] "; chomp (my $resp = <STDIN>); if ( $resp =~ m/^y/i ) { # do nothing } elsif ( $resp =~ m/^n/i ) { next; } elsif ( $resp =~ m/^a/i ) { $prompt = 0; } elsif ( $resp =~ m/^c/i ) { exit; } else { print "Invalid choice.\n"; exit; } } # if prompt rename( $before, $after ); } }

As an example, the following swaps the artist name and song titles for me:


perl renrx "s|(.*?)(\d\d) ([^_]+) _ (.*?)(\.mp3)|$1$2 $4 _ $3$5|"


Maybe-01 Winter Wonderland _ Phantom Planet.mp3 => Maybe-01 Phantom Pl +anet _ Winter Wonderland.mp3 Rename? [Yes|No|Always|Cancel] y Maybe-02 Maybe this Christmas _ Ron Sexsmith.mp3 => Maybe-02 Ron Sexsm +ith _ Maybe this Christmas.mp3 Rename? [Yes|No|Always|Cancel] a

In reply to Renaming Files with Regular Expressions by svsingh

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2017-11-21 05:33 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (295 votes). Check out past polls.