From: Arlen Cuss Date: 2008-04-26T08:29:29+09:00 Subject: Re: pretty folder tree script ------=_Part_8843_9694041.1209166168256 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, On Sat, Apr 26, 2008 at 9:05 AM, John wrote: > > > I think Eivind's solution definitely has some merit, perhaps it's just a > > simple bug that needs to be fixed. I'll look at it tonight. > > Well, it really wont, because > > if FileTest.directory? filename > > is going to throw up on a plain text file. We are, after all, > processing the textfile output of ls -R > > Where Eivind is correct is the way the function calls itself. I think > changing > > if FileTest.directory? filename > > to something like > > if x =~ /.*:$/ > > might work. Then again, the real problem still isn't solved. Nope. Eivind pointed out that your usage of the output of ls -R is incorrect in the first place, which is correct - that function evaluates the directory structure on its own, which is the correct (and much simpler) thing to do. It's just that Eivind forgot to ignore the `.' and `..' entries. Here's the fixed recurse_dir: def recurse_dir(o, dirname) entries = Dir.entries(dirname).sort directories = entries.find_all {|e| FileTest.directory? File.join(dirname, e)}.sort files = entries.find_all {|e| FileTest.file? File.join(dirname, e)}.sort directories.reject! {|a| a =~ /^\./} files.reject! {|a| a =~ /^\./} files.each do |filename| o.puts "#{html_escape(filename)}" end directories.each do |filename| o.puts "
" o.puts "
#{html_escape(filename)}
" recurse_dir o, File.join(dirname, filename) o.puts "
" end end Note that it ignores filenames/directorynames starting with a `.', which avoids the recursion problem. It also lists files first, then directories (both sorted). Use this instead of the entire bit where you process `example'. Here's the entire script pieced back together: http://pastie.caboo.se/187050 It's a lot leaner (without that sample output, I guess). I just tested this on my home directory: I have rather a lot of files, it produced a 2.0MB file in about a minute, which is pretty impressive, I think. And it's not so unreadable, though maybe the styles could use some work. So there we have it. Tell me how it works out for you. This was mostly Eivind's inspiration, though. Arlen ------=_Part_8843_9694041.1209166168256--