From: Vadim Nasardinov Date: 2004-03-25T03:32:18+09:00 Subject: Re: Create a directories list with sub-directories On Wednesday 24 March 2004 03:44, Dirk Einecke wrote: > X-Mail-Count: 95709 > Message-Id: > > I have one question: With your code the files in a folder will be > print out at first. Is it possible to change this that the order is > inverted: at first the folders an after that the files? #!/usr/bin/ruby WSIndent = " " LinedIndent = "| " FileIndent = "+- " def filetree(dir, indent) files = [] dirs = [] Dir.foreach(dir) do |ff| path = File.join(dir, ff) if File.directory?(path) dirs.push(ff) if ff[0] != ?. else files.push(ff) end end if dirs.length > 0 dirs.sort! lastDirSpecial = false dirRange = 0..0 if files.length > 0 dirRange = 0..-1 elsif dirs.length > 1 dirRange = 0..-2 lastDirSpecial = true end nextIndent = indent + LinedIndent thisIndent = indent + FileIndent dirs[dirRange].each do |dd| puts "#{thisIndent}#{dd}/" filetree(File.join(dir, dd), nextIndent) end if lastDirSpecial puts "#{indent + FileIndent}#{dirs[-1]}/" filetree(File.join(dir, dirs[-1]), indent + WSIndent) end end files.sort! fileIndent = indent + FileIndent files.each do |ff| puts "#{fileIndent}#{ff}" end end ARGV.each do |dd| puts "+#{dd}" filetree(dd, "") end