Skip to content

Instantly share code, notes, and snippets.

@denkiwakame
Created January 23, 2014 13:36
Show Gist options
  • Save denkiwakame/8578530 to your computer and use it in GitHub Desktop.
Save denkiwakame/8578530 to your computer and use it in GitHub Desktop.
sortしてrenameするだけ
#!/usr/bin/ruby -Ks
# -Ks:convert code to Shift_JIS
dirpath = ARGV[0] # directory path XXX/
def rename_files(_dirpath)
fnames = Dir.glob _dirpath+'**'
fnames_created = fnames.map { |fname|
[fname, File.mtime(fname)]
}
ascending_fnames = fnames_created.sort {|a,b|
a[1]<=>b[1]
}.map{ |sorted_fname_created|
sorted_fname_created[0]
}
p ascending_fnames
idx = 0
ascending_fnames.each do |fname|
puts fname
idx_str = idx<10 ? '0'+idx.to_s : idx.to_s
File.rename(fname, _dirpath+'/'+idx_str+File.extname(fname))
puts _dirpath+'/'+idx_str+File.extname(fname)
idx += 1
end
end
def test_rename_files
system("mkdir TESTDIR")
for alphabet in ('a'..'z')
system("touch ./TESTDIR/#{alphabet}.png")
sleep(0.1)
end
rename_files('TESTDIR/')
system("rm -rf ./TESTDIR")
end
#test_rename_files
rename_files(dirpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment