Martin Boese
5/12/2008 3:44:00 PM
Call a function recursively when you hit a directory. This will output
something as you mention below, I am just using Dir[*] to get a list of
files:
def scan_files(dir, level=0)
puts "#{' '*level}#{dir}"
Dir["#{dir}/*"].each do |f|
if File.directory?(f) then
scan_files(f, level+1)
else
puts "#{' '*level}#{f}"
end
end
end
scan_files('/home/someone')
Martin
On Monday 12 May 2008 16:16:37 John Locke wrote:
> Hi,
>
> I would like to make the following script in ruby:
>
> Given a directory, for example /home, I would like to take ALL the
> subdirectories and files that are inside /home, and put them in a
> TreeView structure (from Ruby GTK), so I would have all those
> subdirectories and files arranged in that structure that I could then
> display as a graphic object.
>
> I have found how to print all the stuff that is inside my current
> directory:
>
> require 'find'
> Find.find('./') do |f| p f end
>
> I could insert all what that command prints in the TreeView but then it
> would be all at the first level of the TreeView.
>
> So I need to know, at each loop, what is at what level (which is the
> father, which is the son, etc), in order to insert it correctly in the
> TreeView.
>
> In other words, right now I am able to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> etc
>
> And I would like to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> Any ideas?
>
>
> Thanks.