Forum: Ruby Dir

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.
Dir
wes.monk (Guest)
on 2005-11-21 05:58
(Received via mailing list)
Dir.foreach("c:/wes/photos/Bike/*.JPG") {|x| puts "Got#{x}" }

This does not seem to work on my windows xp machine?  Should it? I am
trying to get a list of files with the .JPG extension.

If I take the *.JPG out, I get a list of all of the files in that
directory?  What am I doing wrong? I just want the .JPG's.

Thanks in advance,

wes
kevin.jackson (Guest)
on 2005-11-21 06:10
(Received via mailing list)
removed_email_address@domain.invalid wrote:

>wes
>
>
>
>
Dir.foreach("c:/wes/photos/Bike/") { |x| p "got #{x}" if x[x.length-3,
x.length]=='JPG' }

I'm sure there's a better way, but this should do the trick

Kev
ces.fci (Guest)
on 2005-11-21 06:14
(Received via mailing list)
You could do:
Dir["c:/wes/photos/Bike/*.JPG"].each { |x| puts "Got#{x}" }

change JPG to [JPGjpg] to be ignore the case.

c
dblack (Guest)
on 2005-11-21 06:30
(Received via mailing list)
Hi --

On Mon, 21 Nov 2005, removed_email_address@domain.invalid wrote:

> You could do:
> Dir["c:/wes/photos/Bike/*.JPG"].each { |x| puts "Got#{x}" }
>
> change JPG to [JPGjpg] to be ignore the case.

You'd actually need to do:

   *.[Jj][Pp][Gg]

With *.[JPGjpg] you'll only match files like file.J and file.p.

You could also do:

   *.{jpg,JPG}

if you're sure that those are the only two (i.e., nothing like JpG).


David
nobuyoshi.nakada (Guest)
on 2005-11-21 06:54
(Received via mailing list)
Hi,

At Mon, 21 Nov 2005 13:27:10 +0900,
David A. Black wrote in [ruby-talk:166730]:
> On Mon, 21 Nov 2005, removed_email_address@domain.invalid wrote:
>
> > You could do:
> > Dir["c:/wes/photos/Bike/*.JPG"].each { |x| puts "Got#{x}" }

Or

  Dir.glob("c:/wes/photos/Bike/*.JPG") { |x| puts "Got#{x}" }
bob.news (Guest)
on 2005-11-21 11:40
(Received via mailing list)
nobuyoshi nakada wrote:
>
>   Dir.glob("c:/wes/photos/Bike/*.JPG") { |x| puts "Got#{x}" }

On a Windows box IMHO that should read

Dir.glob("c:\\wes\\photos\\Bike\\*.JPG") { |x| puts "Got#{x}" }

Or - a bit more portable:

Dir[File.join("c:", "wes", "photos", "Bike", "*.jpg")]

Kind regards

    robert
nobuyoshi.nakada (Guest)
on 2005-11-21 11:44
(Received via mailing list)
Hi,

At Mon, 21 Nov 2005 18:37:24 +0900,
Robert K. wrote in [ruby-talk:166758]:
> >   Dir.glob("c:/wes/photos/Bike/*.JPG") { |x| puts "Got#{x}" }
>
> On a Windows box IMHO that should read
>
> Dir.glob("c:\\wes\\photos\\Bike\\*.JPG") { |x| puts "Got#{x}" }

Ruby uses forward-slashes on all platforms.

> Or - a bit more portable:
>
> Dir[File.join("c:", "wes", "photos", "Bike", "*.jpg")]

It yields "c:/wes/photos/Bike/*.JPG" even on Windows.
This topic is locked and can not be replied to.