Reading folder structure on server with Rails - possible?

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…

Never done it but this should get you started.

http://www.ruby-doc.org/core/classes/Pathname.html

Dan

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
509-577-7732
[email protected]

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 :slight_smile:

FWIW the xml schema i want looks like








etc

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???