in reply to Re^2: Removing characters in a file name
in thread Removing characters in a file name

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]

Replies are listed 'Best First'.
Re^4: Removing characters in a file name
by andy2020 (Initiate) on Apr 23, 2020 at 02:42 UTC

    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?

      for my $file ( <*.mp4> ) { if ( $file =~ /-\s*(\d+)\s*-/ ) { rename $file, "[Books] $1.mp4" or die "Cannot rename '$file' b +ecause: $!"; } }

        Thanks a lot. It's working now