Grouping directories by name
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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
|