Examine Folder Contents?

J

Joshua Muheim

Hi all

I'd like Ruby to open a folder, search it for files and then store all
files names of .png files in an array and close the folder. So after
this I can do anything I like with the png files in the array...

How do I do this? Thanks a lot for help. :)
Joshua
 
B

Benjohn Barnes

Hi all

I'd like Ruby to open a folder, search it for files and then store all
files names of .png files in an array and close the folder. So after
this I can do anything I like with the png files in the array...

How do I do this? Thanks a lot for help. :)
Joshua

You'll want the class Dir, and the class method glob.
png_files = Dir.glob(File.join(path_of_folder, '*.png'))

Cheers,
Benj
 
J

Joshua Muheim

You'll want the class Dir, and the class method glob.
png_files = Dir.glob(File.join(path_of_folder, '*.png'))

Cheers,
Benj

Thank you so far. I'm using this in Ruby on Rails and tried the
following:

@images = Dir.glob('/images/content/parties/photo_galleries/1/',
'*.jpg')

Sadly this delivers me an empty array, but there are about 20 jpg files
in this folder... :-/

Thanks for help... Josh
 
S

Stephen Waits

@images = Dir.glob('/images/content/parties/photo_galleries/1/',
'*.jpg')

Try:

@images = Dir.glob('/images/content/parties/photo_galleries/1/
*.jpg')

--Steve
 
D

Daniel Seuthe

Joshua said:
I'd like Ruby to open a folder, search it for files and then store all
files names of .png files in an array and close the folder. So after
this I can do anything I like with the png files in the array...

This is part of a RHTML file for showing all JPEG files as a list:

<% bilder = Dir["*.jpeg"] %>
<ul>
<% bilder.sort.each do |bild| %>
<li><a href="<%= bild %>" /><%= bild.chomp ".jpeg" %></a></li>
<% end %>
</ul>

This is part of a RHTML file for randomly show a picture from a
picture list like above:

<% bilder = Dir["../*.jpeg"] %>
<img src="<%=bilder[rand(bilder.length - 1)] %>" alt="" />

Daniel
 
B

baumanj

This is operating on the actual filesystem, so you have to use real
paths, not the URI for the web application. Try this:

File.join(RAILS_ROOT, 'public')

@images = Dir.glob(File.join(RAILS_ROOT,
'public/images/content/parties/photo_galleries/1/'), '*.jpg')
 
J

Joshua Muheim

Thank you all, guys!

I have now the following array:

-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-01.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-02.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-03.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-04.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-05.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-06.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-07.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-08.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-09.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-10.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-11.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-12.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-13.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-14.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-15.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-16.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-17.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-18.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-19.jpg
-
/Users/Josh/Webwork/PsyGuide.org/public/../config/../public/images/content/parties/photo_galleries/1/2005_10_08_-_claudias_abschieds_party-20.jpg

Is there a way to clean the path (I mean the unnecessary "/..")?

Thanks. :)
 
J

Joshua Muheim

Oh, and how do I cut the RAILS_ROOT away again? So I can use the paths
in the RHTML file again...? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top