# Directory structure of rubytree

**URL:** <https://www.ruby-forum.com/t/directory-structure-of-rubytree/142572>\
**Category:** Rails\
**Created:** [July 9, 2008, 10:55am UTC](https://www.ruby-forum.com/t/directory-structure-of-rubytree/142572 "2008-07-09T10:55:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rutuja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@rutuja](https://www.ruby-forum.com/u/rutuja)\
**Post date:** [July 9, 2008, 10:55am UTC](https://www.ruby-forum.com/t/directory-structure-of-rubytree/142572/1 "2008-07-09T10:55:43Z")

</div>

Hi, I am working on a project in which if we click on to the browse  
button then it should show only the directories,which is possible using  
the rubytree that is a gem.I have require that gem too but i am not  
getting the graphical representation of the directories structure where  
in we can save or retrive the documents.If anybody Knows about this plz  
reply me as soon as possible. It willbe well and good if i get a  
practical examlpe or code for this. Thank you in advance.Hope to get the  
reply fast.thanxs.

---

<div class="post-metadata">

**Author:** ![rutuja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a183cd/32.png) [@rutuja](https://www.ruby-forum.com/u/rutuja)\
**Post date:** [August 9, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/directory-structure-of-rubytree/142572/2 "2008-08-09T12:03:23Z")

</div>

Rutuja Dive wrote:

> Hi, I am working on a project in which if we click on to the browse  
> button then it should show only the directories,which is possible using  
> the rubytree that is a gem.I have require that gem too but i am not  
> getting the graphical representation of the directories structure where  
> in we can save or retrive the documents.If anybody Knows about this plz  
> reply me as soon as possible. It willbe well and good if i get a  
> practical examlpe or code for this. Thank you in advance.Hope to get the  
> reply fast.thanxs.

Below code can be used to convert directory structure in xml.

require ‘rubygems’  
require ‘rexml/document’

class DirectoryStructure  
@@excludes = ["…","."]  
@@doc = REXML::Document.new %{ \<?xml version="1.0" encoding="UTF-8" ?\> }

def self.traverse\_directory(root\_path)

#traverse initial directory structure  
folder = @@doc.add\_element( ‘myroot’,{‘id’ =\> root\_path} )

#call loop\_path with root\_path and parent\_node  
loop\_path(root\_path,folder)  
#create xml file  
open( “final\_directory\_structure.xml”, “w” ) do |f|  
f.puts @@doc  
end

end

def self.loop\_path(path,parent\_node)

dir\_entries= Dir.entries(path)

# don’t need ‘.’,’…’

dir\_entries = dir\_entries-[’.’,’…’]

dir\_entries.each do | entry |

# check if directory or file

if File.directory?(path+"/"+entry)

#Folders Loop -------------------------------------------------  
#Gets the directories of the current node.  
#It changes for each recursive call  
folder = parent\_node.add\_element( ‘folder’ ,{‘label’ =\>entry  
,‘file\_path’ =\> path+"/"+entry } )  
loop\_path( path+"/"+entry, folder )  
else  
#Files Loop ------------------------------------------------------  
file = parent\_node.add\_element( ‘file’ ,{‘label’ =\>entry ,‘file\_path’  
=\> path+"/"+entry } )

end

end

end

#first call to traverse directory  
self.traverse\_directory(“Authentication/Authentication”)

end
