Skip to content

Instantly share code, notes, and snippets.

@lulalala
Created August 10, 2012 10:12
Show Gist options
  • Save lulalala/3313170 to your computer and use it in GitHub Desktop.
Save lulalala/3313170 to your computer and use it in GitHub Desktop.
Partitions numeric folders into 3 hierarchy
# Place under the same folder where the many folders exists, then run the script.
# This will convert folders like 12345678\ to 012\345\678\
require 'fileutils'
Dir.glob("*") do |f|
if not FileTest.directory?(f)
next
end
if f[/^\d+$/].nil?
next
end
if f == '000'
next
end
size = FileTest.size(f)
print f
print " => "
part = ("%09d" % f).scan(/.{3}/)
root = part[0...-1].join("/")
target = part[-1]
new_path = "#{root}/#{target}"
FileUtils.mkdir_p root
FileUtils.mv f, root
File.rename "#{root}/#{f}", new_path
print new_path
if FileTest.exist?(new_path)
puts ' success!'
if FileTest.size(new_path) != size
puts "size different #{size} => #{FileTest.size(new_path)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment