fork download
  1. # remove this line if you want to pass filenames as command line args
  2. ARGV.replace ARGF.each_line(chomp: true).to_a
  3.  
  4. max_digits = 3
  5. max_path_length = ARGV.each.map(&:length).max
  6.  
  7. ARGV.each do |path|
  8. # next unless File.file?(path)
  9. ext = File.extname(path)
  10. basename = File.basename(path, ext)
  11. dir = File.dirname(path)
  12.  
  13. new_base = basename.sub(/([^0-9])(0*[0-9]+)$/) {$1 + $2.rjust(max_digits, "0")}
  14. new_name = new_base + ext
  15. new_path = dir == '.' ? new_name : dir + '/' + new_name
  16. puts sprintf("%-#{max_path_length}s => %s", path, new_path)
  17. end
Success #stdin #stdout 0s 6260KB
stdin
../xy0_0.txt
foo/bar/xy0_0.txt
xy1_1.txt
xy1_01.txt
xy1_10.txt
xy1_012.txt
xy1_0123.txt
xy1_01203.txt
xy1_012030.txt

xy1_012.03.txt
xy1_012.03..txt

xy1_0.txt
xy1_00.txt
xy1_000.txt
xy1_0000.txt

xy1_0.1
xy1_00.01
xy1_000.000
.0
.1
.01
.001
.0000
stdout
../xy0_0.txt      => ../xy0_000.txt
foo/bar/xy0_0.txt => foo/bar/xy0_000.txt
xy1_1.txt         => xy1_001.txt
xy1_01.txt        => xy1_001.txt
xy1_10.txt        => xy1_010.txt
xy1_012.txt       => xy1_012.txt
xy1_0123.txt      => xy1_0123.txt
xy1_01203.txt     => xy1_01203.txt
xy1_012030.txt    => xy1_012030.txt
                  => 
xy1_012.03.txt    => xy1_012.003.txt
xy1_012.03..txt   => xy1_012.03..txt
                  => 
xy1_0.txt         => xy1_000.txt
xy1_00.txt        => xy1_000.txt
xy1_000.txt       => xy1_000.txt
xy1_0000.txt      => xy1_0000.txt
                  => 
xy1_0.1           => xy1_000.1
xy1_00.01         => xy1_000.01
xy1_000.000       => xy1_000.000
.0                => .000
.1                => .001
.01               => .001
.001              => .001
.0000             => .0000