How can I easily bulk rename files with Perl?

后端 未结 9 1100
一生所求
一生所求 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:52

    Run two commands, in this order:

    $ rename 's/File Name (\d)$/File Name 0$1/' *
    $ rename 's/File Name (\d\d)$/File Name 0$1/' *
    

    First one renames everything less than 10 and prepends a zero. The second one renames everything less than 100 and prepends a zero. The result should be three digits for all filenames.

    0 讨论(0)
  • 2021-01-05 17:53

    you could do something using perl or ruby.

    put all this files in the same directory

    dirlisting = DIR.entries('.')
    
    dirListing.each do |file| 
     num = file.match(/\d+$/).to_i
     if num < 100
       find the position where start the number, with index and inject the 0 there.
     end
    end
    
    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题