Forum: Ruby on Rails reading folder structure on server with Rails - possible?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5a992f256f765f8ee3dbb2af9d3db4e0?d=identicon&s=25 Robbie (Guest)
on 2006-01-18 07:44
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...
66b5b555164e1904406464ad677e3e72?d=identicon&s=25 Dan Wright (Guest)
on 2006-01-18 08:17
(Received via mailing list)
Never done it but this should get you started.

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

Dan
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-18 18:09
(Received via mailing list)
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 Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-18 18:33
(Received via mailing list)
Ezra Zygmuntowicz wrote:
<snip>
>    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 :-)
5a992f256f765f8ee3dbb2af9d3db4e0?d=identicon&s=25 Robbie Shepherd (robbie)
on 2006-01-27 05:10
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???
5a992f256f765f8ee3dbb2af9d3db4e0?d=identicon&s=25 Robbie Shepherd (robbie)
on 2006-01-27 05:13
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
This topic is locked and can not be replied to.