Robbie
2006-Jan-18 06:08 UTC
[Rails] 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... -- Posted via http://www.ruby-forum.com/.
Dan Wright
2006-Jan-18 06:39 UTC
[Rails] reading folder structure on server with Rails - possible?
Never done it but this should get you started. http://www.ruby-doc.org/core/classes/Pathname.html Dan On 1/18/06, Robbie <robbie.shepherd@gmail.com> wrote:> > 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... > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/a3e6f2f9/attachment.html
Ezra Zygmuntowicz
2006-Jan-18 16:30 UTC
[Rails] reading folder structure on server with Rails - possible?
On Jan 17, 2006, at 10:44 PM, Robbie wrote:> 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... > > -- >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 Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com
Alex Young
2006-Jan-18 16:54 UTC
[Rails] reading folder structure on server with Rails - possible?
Ezra Zygmuntowicz wrote: <snip>> 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 > endUhh... 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 :-) -- Alex
robbie shepherd
2006-Jan-27 04:10 UTC
[Rails] Re: reading folder structure on server with Rails - possible
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 = ''<struct>''+"\n" + render_directory(@path,0) + ''</struct>'' 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 += ''<node label="'' + file + ''" />''+"\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??? -- Posted via http://www.ruby-forum.com/.
robbie shepherd
2006-Jan-27 04:13 UTC
[Rails] Re: reading folder structure on server with Rails - possible
FWIW the xml schema i want looks like <struct> <node label="...dir1"> <node label=".../dir1/subdir"> <node label=".../dir1/subdir/file1" /> </node> <node label="...dir2"> <node label=".../dir2/fileXYZ" /> </node> </struct> etc -- Posted via http://www.ruby-forum.com/.