#!/usr/bin/ruby target_dirs = [] Dir::open('.').each{|f| next unless f =~ /^\d+$/ target_dirs << f } dir_flist = [] target_dirs.sort{|a, b| a <=> b}.each{|dir| dir_flist << [dir, Dir::open(dir).reject{|f| f !~ /^img_\d+.jpg$/}.sort{|x, y| x <=> y}] } out_dir = ARGV.shift || "out" unless FileTest::directory?(out_dir) then Dir::mkdir(out_dir) end offset = 0 dir_flist.each{|dir, flist| $stderr.puts "#{dir} => offset:#{offset}" flist.each{|f| f =~ /^img_(\d+).jpg$/ new_f = sprintf("img_%05d.jpg", $1.to_i + offset) command = "cp #{dir}/#{f} #{out_dir}/#{new_f}" $stderr.puts command `#{command}` } offset += flist.size }