Forum: Ruby on Rails browse directory

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.
Fa649cbe578868ea93801ab4bbedcd5c?d=identicon&s=25 thomas.tmp (Guest)
on 2005-11-21 23:15
(Received via mailing list)
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.
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-11-21 23:19
(Received via mailing list)
On Mon, 2005-11-21 at 23:13 +0100, Thomas Balthazar wrote:
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.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
22e571c8725e328d21e9ac38aeab9916?d=identicon&s=25 Michael Palmer (mariachi77)
on 2005-12-08 11:06
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@lists.rubyonrails.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
Fa649cbe578868ea93801ab4bbedcd5c?d=identicon&s=25 thomas.tmp (Guest)
on 2005-12-08 11:21
(Received via mailing list)
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.
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 phurley (Guest)
on 2005-12-08 15:27
(Received via mailing list)
> On 12/8/05, Michael Palmer <mike@stroke7.com> 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.
22e571c8725e328d21e9ac38aeab9916?d=identicon&s=25 Michael Palmer (mariachi77)
on 2005-12-09 10:28
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-12-09 17:05
(Received via mailing list)
Try using double quotes...#{Dir.pwd} won't get evaluated inside single
quotes

files = Dir["#{Dir.pwd}/public/downloads/*"]
22e571c8725e328d21e9ac38aeab9916?d=identicon&s=25 Michael Palmer (mariachi77)
on 2005-12-09 19:21
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/*"]
Fa649cbe578868ea93801ab4bbedcd5c?d=identicon&s=25 Thomas Balthazar (Guest)
on 2005-12-13 10:15
(Received via mailing list)
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.
This topic is locked and can not be replied to.