Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Removing characters in a file name

by andy2020 (Initiate)
on Apr 22, 2020 at 22:06 UTC ( [id://11115907]=perlquestion: print w/replies, xml ) Need Help??

andy2020 has asked for the wisdom of the Perl Monks concerning the following question:

Here's my input

Jane Doe & John Doe - 124 - How to Invest.mp4

Here's what I want in the end

[Books] 124.mp4

I can't figure out how to do it. Can you help me please?

Thanks

Replies are listed 'Best First'.
Re: Removing characters in a file name
by choroba (Cardinal) on Apr 22, 2020 at 22:21 UTC
    The trivial way would be
    rename 'Jane Doe & John Doe - 124 - How to Invest.mp4', '[Books] 124.mp4' or die $!;

    See rename. Or is there something more you haven't shown?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I'm sorry for not being clear before. There are hundreds of files in the directory and not just one. All names are consistent in naming such as below

      Jane Doe & John Doe - 124 - How to Invest.mp4 Jane Smith & John Smith - 100 - Stocks.mp4 Jane Black & John Black - 90 - Bonds.mp4

      Here's what the end results should be

      [Books] 124.mp4 [Books] 100.mp4 [Books] 90.mp4

      Thanks

        Loop over the files, you can use glob to get the list. Make sure there is a number to rename to:
        for my $file (glob '*.mp4') { if ($file =~ /^[^0-9]*([0-9]+)[^0-9]*.mp4/) { rename $file, "[Books] $1.mp4" or die "$file: $!"; } elsif ($file =~ /[0-9].*4/) { die "Several numbers found in $file."; } else { die "No number found in $file." } }

        You can replace the second and third die with a warn if you just want to skip the files that weren't renamed.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        I like the idea from choroba, but I would perhaps do it a bit differently. The OP doesn't specify what to do if the rename cannot happen. I would just "die" in that case with an error message. I would only process .mp4 files that match the pattern, but again that is up to the OP to decide how to handle an .mp4 file that doesn't match the pattern.
        use strict; use warnings; foreach my $file ('Jane Doe & John Doe - 124 - How to Invest.mp4', 'Jane Smith & John Smith - 100 - Stocks.mp4', 'Jane Black & John Black - 90 - Bonds.mp4', 'Jane Black & John Black - 120 - Bonds2Invest.mp4', 'Jane Black & John Black - 123 - Bonds 2 Invest.mp4' +, ) { if ( (my $num) = $file =~ / (\d+) - [\w ]+.mp4$/ ) { #rename $file, "[Books] $num.mp4" or die "Cannot rename $file to + $num.mp4 - probable cause: duplicate number: $!"; print "$num.mp4\n"; } } __END__ 124.mp4 100.mp4 90.mp4 120.mp4 123.mp4
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11115907]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-16 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found