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…
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:
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 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???
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.