in reply to Removing characters in a file name

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]

Replies are listed 'Best First'.
Re^2: Removing characters in a file name
by andy2020 (Initiate) on Apr 22, 2020 at 22:45 UTC

    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]

        Thanks for the reply. It's working now. Unfortunately I found out that I have some files that have the following format too

        JohnDoe-JaneDoe-399-1080p.mp4

        So the only common thing of this new file format with the previous format is the number between the two dashes. There could be spaces before or after the two dashes too. So the two formats that I have will be below

        JohnDoe-JaneDoe-399-1080p.mp4 John Doe & Jane Doe - 458 - Bonds.mp4

        As you can see, there could be spaces before and after the dashes so the only consistent thing between the two file naming formats is there will be a number between two dashes

        Is there a way to modify the if statement to accommodate both file formats?

      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