Hello everyone I''m trying to make a "directory chooser" for a form, so that the user can choose a subdirectory (via dropdown) of a specified root directory (under public/images) on the server. Sooo... how do I get an array or a hash of all the subdirectories in a specified directories? And: how can I then get an array or a hash of all the files that are in that subdir? Thanks a lot! al --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
cremes.devlist-ee4meeAH724@public.gmane.org
2006-Nov-20 12:35 UTC
Re: Getting all directories in a specified directory
On Nov 15, 2006, at 8:28 AM, al schmid wrote:> > Hello everyone > > I''m trying to make a "directory chooser" for a form, so that the user > can choose a subdirectory (via dropdown) of a specified root directory > (under public/images) on the server. Sooo... how do I get an array > or a > hash of all the subdirectories in a specified directories? > And: how can I then get an array or a hash of all the files that > are in > that subdir?Try this: def parse_dir(path) dirs, files = [], [] Dir.entries(path).each do |p| dirs << p if File.directory?(p) files << p if File.file?(p) end dirs[0...2] = [] # removes ''.'' and ''..'' directories (if on UNIX) [dirs,files] end directories, files = parse_dir(some_path) Call additional times with a new path to get subdirectory information. cr --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Sorry if this is coming to the list a second time... I''m new to Ruby on Rails and also new here, so apologies if this is something obvious I''m missing. I''m also trying to list the contents of a directory and separate them into files and dirs. The ruby script I''ve written, as well as the one here, both work fine when run from the terminal. However, when I try to use either in Rails, the app only recognizes .. and . as directories and .DS_Store as a file, even though it can list the complete contents of the directory. Any thoughts? Thanks for any help. cheers dafydd cremes.devlist-ee4meeAH724@public.gmane.org wrote:> On Nov 15, 2006, at 8:28 AM, al schmid wrote: > > > > > Hello everyone > > > > I''m trying to make a "directory chooser" for a form, so that the user > > can choose a subdirectory (via dropdown) of a specified root directory > > (under public/images) on the server. Sooo... how do I get an array > > or a > > hash of all the subdirectories in a specified directories? > > And: how can I then get an array or a hash of all the files that > > are in > > that subdir? > > Try this: > > def parse_dir(path) > dirs, files = [], [] > Dir.entries(path).each do |p| > dirs << p if File.directory?(p) > files << p if File.file?(p) > end > dirs[0...2] = [] # removes ''.'' and ''..'' directories (if on UNIX) > [dirs,files] > end > > directories, files = parse_dir(some_path) > > Call additional times with a new path to get subdirectory information. > > cr--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dafydd wrote:> I''m also trying to list the contents of a directory and separate them > into files and dirs. The ruby script I''ve written, as well as the one > here, both work fine when run from the terminal. However, when I try > to use either in Rails, the app only recognizes .. and . as directories > and .DS_Store as a file, even though it can list the complete contents > of the directory.Perhaps this code snippet will help. Also try logging/outputting the result of Dir.pwd, i.e. where is your current working directory under each circumstance? class Dir def self.crawl( path, max_depth=nil, include_directories=false, depth=0, &block ) return if max_depth && depth > max_depth begin if File.directory?( path ) yield( path, depth ) if include_directories files = Dir.entries( path ).select{ |f| f[0,1]!=''.'' } unless files.empty? files.collect!{ |file_path| Dir.crawl( path+''/''+file_path, max_depth, include_directories, depth+1, &block ) }.flatten! end return files else yield( path, depth ) end rescue SystemCallError => the_error warn "ERROR: #{the_error}" end end end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey thanks! I have to admit the code snippet is a bit beyond my grasp at the moment, and I''m gonna have to spend some time figuring it out, but in the meantime I followed your current directory tip and taht fixed the trouble. As far as I can figure, I needed to actually bbe working in the directory - it wasn''t enough to just provide the pathname. Thanks again. cheers dafydd On 12/15/06, Phrogz <gavin-XtLdkLkwz3ZWk0Htik3J/w@public.gmane.org> wrote:> > dafydd wrote: > > I''m also trying to list the contents of a directory and separate them > > into files and dirs. The ruby script I''ve written, as well as the one > > here, both work fine when run from the terminal. However, when I try > > to use either in Rails, the app only recognizes .. and . as directories > > and .DS_Store as a file, even though it can list the complete contents > > of the directory. > > Perhaps this code snippet will help. Also try logging/outputting the > result of Dir.pwd, i.e. where is your current working directory under > each circumstance? > > class Dir > def self.crawl( path, max_depth=nil, include_directories=false, > depth=0, &block ) > return if max_depth && depth > max_depth > begin > if File.directory?( path ) > yield( path, depth ) if include_directories > files = Dir.entries( path ).select{ |f| f[0,1]!=''.'' } > unless files.empty? > files.collect!{ |file_path| > Dir.crawl( path+''/''+file_path, max_depth, > include_directories, depth+1, &block ) > }.flatten! > end > return files > else > yield( path, depth ) > end > rescue SystemCallError => the_error > warn "ERROR: #{the_error}" > end > end > > end > > > > >-- www.sideshowmedia.ca skype: chickeninthegrass --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---