Topic: Iterate trough folder with all sub-folders

Well is there a simple way to get an array with all the subfolders and files in it ?

Simple function or plugin, cant find anything.

Re: Iterate trough folder with all sub-folders

There is the Ruby File class, google it, it should help you out.

Re: Iterate trough folder with all sub-folders

>>   path = "/home/max/"
=> "/home/max/"
>>   child_folders = Dir[File.join(path, "*")].select{|file| File.ftype(file) == "directory"}.collect{|name| name.split("/").last}
=> ["Templates", "Music", "Desktop", "dev", "Public", "E-Books", "Videos", "work", "Documents", "Downloads", "Pictures"]
>>   child_files = Dir[File.join(path, "*")].select{|file| File.ftype(file) == "file"}.collect{|name| name.split("/").last}
=> ["2008_07_10_live.sql", "finders_with_logging.rb", "2008_07_18_live.sql"]

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Iterate trough folder with all sub-folders

Simplest way I know is like this:

all_files = Dir["/home/jerod/**/**"]

That will recursively put all filenames into the all_files array. I wrote a short blog post about this recently which may be of use for reference. Link

"Yeah, well, you know, that's just, like, your opinion, man." -El Duderino

http://jerodsanto.net

Re: Iterate trough folder with all sub-folders

I was looking for something like this as well. I found this site as well where it seems a lot simpler for a begginer like me. If anyone else finds this (I was here, hehe) but here is the link:

http://www.winn.ws/archives/118

Thanks to the sant0sk1 and others