Skip to content

Instantly share code, notes, and snippets.

@b-coimbra
Last active June 3, 2017 20:55
Show Gist options
  • Save b-coimbra/6fe81b3067b2767d5d39cbe341bc7ccb to your computer and use it in GitHub Desktop.
Save b-coimbra/6fe81b3067b2767d5d39cbe341bc7ccb to your computer and use it in GitHub Desktop.
sort files numerically in increasing order
#!/usr/bin/env ruby
->(i) {->(_) {Dir[$_=(_.nil? ? '.' : _) + '/*'].each_with_index {|f,i| f.to_enum(:scan, /(?<type>\.(png|jpg|jpeg|gif|webm|mp4|pdf))$/im). \
map {p "[#{-~i}/#{Dir["#{_}/*"].length}] "+f;$_=f; test(?e, ($_) + i.to_s + $1) ? next : \
File.rename(f, File.dirname(f) + File::SEPARATOR + (-~i).to_s + $~[:type])}}} \
::(ARGV[0])}.(0) && at_exit {system "echo Done! && explorer #{$_.gsub(/\/[(\w|\d)\.\w\d]+$/i,'')}"} \
rescue Errno::EACCESS abort "Cant sort this folder :("
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment