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.
thomas.tmp (Guest)
on 2005-11-22 00: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 B..
Jamie M. (Guest)
on 2005-11-22 00:19
(Received via mailing list)
On Mon, 2005-11-21 at 23:13 +0100, Thomas B. wrote:
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> 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
Michael P. (Guest)
on 2005-12-08 12: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 B. wrote:
>> _______________________________________________
>> Rails mailing list
>> removed_email_address@domain.invalid
>> 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
thomas.tmp (Guest)
on 2005-12-08 12: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 B..
phurley (Guest)
on 2005-12-08 16:27
(Received via mailing list)
> On 12/8/05, Michael P. <removed_email_address@domain.invalid> 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.
Michael P. (Guest)
on 2005-12-09 11: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
christopher.k.hall (Guest)
on 2005-12-09 18:05
(Received via mailing list)
Try using double quotes...#{Dir.pwd} won't get evaluated inside single
quotes

files = Dir["#{Dir.pwd}/public/downloads/*"]
Michael P. (Guest)
on 2005-12-09 20: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/*"]
Thomas B. (Guest)
on 2005-12-13 11: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.