# Browse directory

**URL:** <https://www.ruby-forum.com/t/browse-directory/48533>\
**Category:** Rails\
**Created:** [November 21, 2005, 11:15pm UTC](https://www.ruby-forum.com/t/browse-directory/48533 "2005-11-21T23:15:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [November 21, 2005, 11:15pm UTC](https://www.ruby-forum.com/t/browse-directory/48533/1 "2005-11-21T23:15:41Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [November 21, 2005, 11:19pm UTC](https://www.ruby-forum.com/t/browse-directory/48533/2 "2005-11-21T23:19:42Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 8, 2005, 11:06am UTC](https://www.ruby-forum.com/t/browse-directory/48533/3 "2005-12-08T11:06:15Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 8, 2005, 11:21am UTC](https://www.ruby-forum.com/t/browse-directory/48533/4 "2005-12-08T11:21:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 9, 2005, 10:28am UTC](https://www.ruby-forum.com/t/browse-directory/48533/5 "2005-12-09T10:28:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 9, 2005, 5:05pm UTC](https://www.ruby-forum.com/t/browse-directory/48533/6 "2005-12-09T17:05:45Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 8, 2005, 3:27pm UTC](https://www.ruby-forum.com/t/browse-directory/48533/7 "2005-12-08T15:27:07Z")

</div>

> On 12/8/05, Michael P. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 9, 2005, 7:21pm UTC](https://www.ruby-forum.com/t/browse-directory/48533/8 "2005-12-09T19:21:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 13, 2005, 10:15am UTC](https://www.ruby-forum.com/t/browse-directory/48533/9 "2005-12-13T10:15:42Z")

</div>

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.
