How can I easily bulk rename files with Perl?

后端 未结 9 1098
一生所求
一生所求 2021-01-05 17:25

I have a lot of files I\'m trying to rename, I tried to make a regular expression to match them, but even that I got stuck on the files are named like:

<
9条回答
  •  隐瞒了意图╮
    2021-01-05 17:57

    use strict;
    use File::Copy;
    
    my @files = glob 'File*Name*';
    
    foreach my $filename (@files) {
        if ($filename =~ m`^.*File.*Name.*?(\d+)`) {
            my $number = $1;
            next if ($number > 99);
            rename $filename, sprintf("FileName%03d",$number);
        }
    }
    

提交回复
热议问题