As is, this code will create an array of arrays – Directories with the same 6 first characters will be grouped in the same sub-array. I am using this for log directories, name format 20081125 for instance – you can just replace the logic to be whatever you need, of course.

dirs = []
Dir['*'].each do |item|
  if File.directory?(item)
    if dirs.empty?
      dirs << [item]
      next
    end
    inserted = false
    dirs.each_with_index do |list_of_folders, index|
      if list_of_folders[0].to_s[0..5] == item[0..5]
        dirs[index] << item
        inserted = true
      end
    end
    dirs << [item] if !inserted
  end
end
   
© 2011 Seven steps Suffusion theme by Sayontan Sinha