Hi Railers, I would like to parse the content of a directory in order to display its content (in fact, I would like to list all the images in a folder to display them in a gallery). Do you have any clue about the best way to begin? Many thanks, Thomas Balthazar.
On Mon, 2005-11-21 at 23:13 +0100, Thomas Balthazar wrote:> Hi Railers, > > I would like to parse the content of a directory in order to display > its content (in fact, I would like to list all the images in a folder > to display them in a gallery). > > Do you have any clue about the best way to begin? > > Many thanks, > Thomas Balthazar. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsDir[''/path/to/images/*''] will give you an array containing all the files (and directories) inside that location. To grab all the images, you could do something like this: files = Dir[''/path/to/images/*''] images = files.select { |file| /(jpg|png|gif)$/ =~ file } puts images.inspect gives you: ["/path/to/images/foo.gif", "/path/to/images/bar.png"] - Jamie
G''day, I''ve tried this and I keep getting an empty array. Is there something missing? Mike jamie wrote:> On Mon, 2005-11-21 at 23:13 +0100, Thomas Balthazar wrote: >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > Dir[''/path/to/images/*''] will give you an array containing all the files > (and directories) inside that location. To grab all the images, you > could do something like this: > > files = Dir[''/path/to/images/*''] > images = files.select { |file| /(jpg|png|gif)$/ =~ file } > puts images.inspect > > gives you: > > ["/path/to/images/foo.gif", "/path/to/images/bar.png"] > > - Jamie-- Posted via http://www.ruby-forum.com/.
Hello Mike, It worked fine for me. Are you sure the folder you browse contains jpg, png or gif files? If you give me more details, maybe I can help you. Regards, Thomas Balthazar. On 12/8/05, Michael Palmer <mike-b3K1ntNyKhlBDgjK7y7TUQ@public.gmane.org> wrote:> G''day, > > I''ve tried this and I keep getting an empty array. Is there something > missing? > > Mike > > jamie wrote: > > On Mon, 2005-11-21 at 23:13 +0100, Thomas Balthazar wrote: > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > Dir[''/path/to/images/*''] will give you an array containing all the files > > (and directories) inside that location. To grab all the images, you > > could do something like this: > > > > files = Dir[''/path/to/images/*''] > > images = files.select { |file| /(jpg|png|gif)$/ =~ file } > > puts images.inspect > > > > gives you: > > > > ["/path/to/images/foo.gif", "/path/to/images/bar.png"] > > > > - Jamie > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> On 12/8/05, Michael Palmer <mike-b3K1ntNyKhlBDgjK7y7TUQ@public.gmane.org> wrote: > > I''ve tried this and I keep getting an empty array. Is there something > > missing?If you are not using an absolute path, you may want to double check that your current directory is what you expect it to be (you can throw it up in a view with: <%= h(Dir.pwd) %> For the sake of debugging.
Alright, this is what I''ve got right now: <% files = Dir[''#{Dir.pwd}/public/downloads/*''] musicclips = files.select { |file| /(mp3|MP3)$/ =~ file } puts musicclips.inspect %> I''m actually looking for mp3 files, not images, but I keep getting a blank. I also tried using RAILS_ROOT as follows, again to no avail: files = Dir[''#{RAILS_ROOT}/public/downloads/*''] I''ve tried removing the trailing backslash, and I also tried looking for ANY file, regardless of file extension: <% files = Dir[''#{Dir.pwd}/public/downloads/*''] files.each {|name| puts "Got #{name}"} %> I''m using InstantRails on winXP SP2. Thanks for your help! Mike -- Posted via http://www.ruby-forum.com/.
Try using double quotes...#{Dir.pwd} won''t get evaluated inside single quotes files = Dir["#{Dir.pwd}/public/downloads/*"] On 12/9/05, Michael Palmer <mike-b3K1ntNyKhlBDgjK7y7TUQ@public.gmane.org> wrote:> > Alright, this is what I''ve got right now: > > <% > files = Dir[''#{Dir.pwd}/public/downloads/*''] > musicclips = files.select { |file| /(mp3|MP3)$/ =~ file } > puts musicclips.inspect > %> > > I''m actually looking for mp3 files, not images, but I keep getting a > blank. > > I also tried using RAILS_ROOT as follows, again to no avail: > files = Dir[''#{RAILS_ROOT}/public/downloads/*''] > > I''ve tried removing the trailing backslash, and I also tried looking for > ANY file, regardless of file extension: > > <% > files = Dir[''#{Dir.pwd}/public/downloads/*''] > files.each {|name| puts "Got #{name}"} > %> > > I''m using InstantRails on winXP SP2. Thanks for your help! > > Mike > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Christopher, Tried with the double quotes -- no change unfortunately. Mike christopher.k.hall wrote:> Try using double quotes...#{Dir.pwd} won''t get evaluated inside single > quotes > > files = Dir["#{Dir.pwd}/public/downloads/*"]-- Posted via http://www.ruby-forum.com/.
Hi Michael, Very strange. This simple ruby program worked for me : --- #!/usr/bin/ruby files = Dir["#{Dir.pwd}/*"] files.each {|name| puts "Got #{name}"} puts "#{Dir.pwd}/*" -- Try to run it as a simple ruby program, not in rails. What do you get with puts "#{Dir.pwd}/*"? Thomas. On 12/9/05, Michael Palmer <mike-b3K1ntNyKhlBDgjK7y7TUQ@public.gmane.org> wrote:> > Hi Christopher, > > Tried with the double quotes -- no change unfortunately. > > Mike > > christopher.k.hall wrote: > > Try using double quotes...#{Dir.pwd} won''t get evaluated inside single > > quotes > > > > files = Dir["#{Dir.pwd}/public/downloads/*"] > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails