Browse directory

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 B…

On Mon, 2005-11-21 at 23:13 +0100, Thomas B. wrote:


Rails mailing list
[email protected]
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

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 B. wrote:


Rails mailing list
[email protected]
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

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 B…

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

Try using double quotes…#{Dir.pwd} won’t get evaluated inside single
quotes

files = Dir["#{Dir.pwd}/public/downloads/*"]

On 12/8/05, Michael P. [email protected] 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.

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/*"]

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.