Dir


#1

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


#2

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


#3

You could do:
Dir[“c:/wes/photos/Bike/*.JPG”].each { |x| puts “Got#{x}” }

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

c


#4

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


#5

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}” }


#6

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

#7

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.