Forum: Ruby Examine Folder Contents?

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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-04-01 14:41
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
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-01 14:50
(Received via mailing list)
On 1 Apr 2006, at 13:41, Joshua Muheim wrote:

> 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
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-04-01 18:06
> 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
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-04-01 18:07
Ups sorry, it doesn't return an empty array but the following error:

cannot convert String into Integer
280b41a88665fd8c699e83a9a25ef949?d=identicon&s=25 Stephen Waits (Guest)
on 2006-04-01 18:27
(Received via mailing list)
On Apr 1, 2006, at 8:06 AM, Joshua Muheim wrote:

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

Try:

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

--Steve
A61ec111d0537333696ff7379b8ed781?d=identicon&s=25 Daniel Seuthe (Guest)
on 2006-04-02 00:14
(Received via mailing list)
Joshua Muheim schrieb:
> 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
54f9c5392e17646bc29fe6435b55f609?d=identicon&s=25 baumanj@gmail.com (Guest)
on 2006-04-02 17:29
(Received via mailing list)
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')
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-04-04 19:54
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. :-)
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-04-04 20:03
Oh, and how do I cut the RAILS_ROOT away again? So I can use the paths
in the RHTML file again...? :-)
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-04 20:06
(Received via mailing list)
On Wed, 5 Apr 2006, Joshua Muheim wrote:

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

   list_of_paths.map!{|path| File.expand_path path}

-a
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2006-04-04 20:46
(Received via mailing list)
You might want to look at Pathname
(http://ruby-doc.org/stdlib/libdoc/pathname/rdoc/cl...)

It wraps alot of the File related libs into a nice package

Cheers
This topic is locked and can not be replied to.