From: Stefan Rusterholz Date: 2007-07-06T12:07:47+09:00 Subject: Re: fugly hex Sure require 'fileutils' # as Gregory Brown already stated 65536.times { |x| FileUtils.mkdir("new/%x/%x/%x/%x" % [x >> 12, x >> 8, x >> 4, x].map { |x| x & 0xf }) } Have fun Stefan Giles Bowkett wrote: > Recently I got a hard drive back from a drive recovery place after it > had died a messy, violent, public death, like a Sopranos guest star. > > I had to restore 65,536 directories. > > Each directory contained some unknown (possibly zero) number of files. > > The files were stored in directories with single-character names > running 0-f (hexadecimal). > > My xargs fu was then too weak to handle this with a simple cp -r in > Unix. The argument list was too long. Additionally, the 65,536 > directories did not yet exist in the new location. > > The solution was pretty quick to implement: > > hex = (0..9).to_a + %w{a b c d e f} > > hex.each do |first| > system "mkdir /new/#{first}" > hex.each do |second| > system "mkdir /new/#{first}/#{second}" > hex.each do |third| > system "mkdir /new/#{first}/#{second}/#{third}" > hex.each do |fourth| > system "mkdir /new/#{first}/#{second}/#{third}/#{fourth}" > system "cp /original/#{first}/#{second}/#{third}/#{fourth}/* > /new/#{first}/#{second}/#{third}/#{fourth}/" > end > end > end > end > > The hardest part was being satisfied with the implementation, and > resisting the urge to improve it. There's a great deal of repetition > there. > > How can I make it prettier? > -- Posted via http://www.ruby-forum.com/.