# Reading folder structure on server with Rails - possible?

**URL:** <https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710>\
**Category:** Rails\
**Created:** [January 18, 2006, 7:44am UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710 "2006-01-18T07:44:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [January 18, 2006, 7:44am UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710/1 "2006-01-18T07:44:54Z")

</div>

I’m building an extranet which has project folders allocated for each  
client and client project on a webserver.

I’m hoping to be able to read a directory’s content if I give it a path  
like “/clients/[CLIENTNAME]/[PROJECTNAME]” (where /clients/… would  
reside inside the rails public folder I guess)

preferably recursively so it will read the contents of any  
subdirectories as well.

Is it even possible? How do I go about reading contents of folders?  
Ideally I’d like to be able to add folders and upload files as well, but  
one thing at a time…

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [January 18, 2006, 8:17am UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710/2 "2006-01-18T08:17:23Z")

</div>

Never done it but this should get you started.

[http://www.ruby-doc.org/core/classes/Pathname.html](http://www.ruby-doc.org/core/classes/Pathname.html)

Dan

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [January 18, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710/3 "2006-01-18T18:09:32Z")

</div>

On Jan 17, 2006, at 10:44 PM, Robbie wrote:

> Is it even possible? How do I go about reading contents of folders?  
> Ideally I’d like to be able to add folders and upload files as  
> well, but  
> one thing at a time…
> 
> –

Robbie-

```
This little snippet will recursively list all files in a directory

```

for you. It will also check to make sure they are files and not  
directories being listed.

require ‘find’

def recurse\_files(dir)  
file\_list = ‘’  
Find.find(dir) do |file|  
unless test(?d, file)  
file\_list \<\< file  
end  
end  
file\_list  
end

```
Now you can do the following in your controller to fill an array

```

called @files with all the files in that directory and any directory  
beneath it:

@files = recurse\_files(“#{RAILS\_ROOT}/public/clients”)

```
Just remember that you must give the full path on the filesystem or

```

start with RAILS\_ROOT like the example. Hope this helps.

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [January 18, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710/4 "2006-01-18T18:33:45Z")

</div>

Ezra Z. wrote:

> file\_list = ‘’  
> Find.find(dir) do |file|  
> unless test(?d, file)  
> file\_list \<\< file  
> end  
> end  
> file\_list  
> end  
> Uhh… Surely you mean:

def recurse\_files(dir)  
Dir.chdir(dir){ Dir[’\*\*/\*’].select{|f| File.file? f} }  
end

(sorry… I’m a sucker for one-liners 🙂

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [January 27, 2006, 5:13am UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710/5 "2006-01-27T05:13:53Z")

</div>

FWIW the xml schema i want looks like

etc

---

<div class="post-metadata">

**Author:** ![robbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/2acd7d/32.png) [@robbie](https://www.ruby-forum.com/u/robbie)\
**Post date:** [January 27, 2006, 5:10am UTC](https://www.ruby-forum.com/t/reading-folder-structure-on-server-with-rails-possible/52710/6 "2006-01-27T05:10:16Z")

</div>

ok…thanks for the replies, but I’m totally stuck on how to do this  
now.  
I want to render out an entire folder structure into xml, and I’ve  
attempted to use the above code to recursively do this, but I keep  
getting an error when the function calls itself

def doSomething  
…  
@path = “#{RAILS\_ROOT}/public/clients/” + dirname + “/” + @jobnumber +  
“/”

@files = ‘’+"\n" + render\_directory(@path,0) + ‘’

end

def render\_directory(dir,num)  
if num==0  
str = ‘’  
end  
Find.find(dir) do |file|  
if test(?d, file)  
if file != dir  
render\_directory(file + “/”,1) #dies here!  
end  
else  
str += ‘’+"\n"

```
		end
     end
     return str
end

```

the error I get is:  
NoMethodError in Project#get\_folders

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.+

Someone out there must have some nice easy code to put a folder  
structure into a nested xml format???
