| Listing a Directory Tree 
 
	 
	
		| Author | Message |   
		| Cervantes 
 
  
 
 
 | 
			
				|  Posted: Mon Jun 26, 2006 8:28 pm    Post subject: Listing a Directory Tree |  |   
				| 
 |  
				| This code adds a class method to the Dir class that generates a nice text graphic for a directory. For example, using this program on my Rubidium directory produces: 
 	  | tree: |  	  | 
|-- plugins
 |   |-- help
 |   |   |-- help.txt
 |   |   |-- help.rb
 |   |-- about
 |   |   |-- about.txt
 |   |   |-- about.rb
 |   |-- kickban.rb
 |   |-- license.rb
 |   |-- clear.rb
 |-- logs
 |-- img
 |   |-- rubidium-splash.png
 |-- testing
 |   |-- ansicolor.rb
 |   |-- colourful.rb
 |-- README.txt
 |-- IRC.rb
 |-- client.rb
 |-- plugin.rb
 |-- run.rb
 
 | 
 I took the idea from the 'tree' package in the Ubuntu (and probably others) repositories.
 
 The source:
 
 	  | Ruby: |  	  | 
class Dir
 def Dir.print_tree(dir = ".", nesting = 0)
 Dir.foreach(dir) do |entry|
 next if entry =~ /^\.{1,2}/   # Ignore ".", "..", or hidden files
 puts "|   " * nesting + "|-- #{entry}"
 if File.stat(d = "#{dir}#{File::SEPARATOR}#{entry}").directory?
 print_tree(d, nesting + 1)
 end
 end
 end
 end
 
 | 
 Use it like so:
 
 	  | Ruby: |  	  | 
Dir.print_tree                                      # print a tree of the current directory
 Dir.print_tree("plugins")                      # print a tree of the "plugins" subdirectory
 Dir.print_tree("/usr/lib/ruby/1.8/irb")    # print a tree of the "irb" directory in the ruby library
 
 | 
 Thanks to wtd who helped me with something similar earlier.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| Null 
 
  
 
 
 | 
			
				|  Posted: Tue Jun 27, 2006 11:33 am    Post subject: (No subject) |  |   
				| 
 |  
				| That's great! 
 Thanks.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| rdrake 
 
  
 
 
 | 
			
				|  Posted: Tue Jun 27, 2006 1:36 pm    Post subject: (No subject) |  |   
				| 
 |  
				| Very nice, Cervantes.  Now, back to the IRC channel with you  . |  
				|  |  |   
		|  |  |  
	  
		|  |   
		|  |  
 |