# Dir

**URL:** <https://www.ruby-forum.com/t/dir/93285>\
**Category:** Ruby\
**Created:** [November 21, 2005, 4:58am UTC](https://www.ruby-forum.com/t/dir/93285 "2005-11-21T04:58:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 4:58am UTC](https://www.ruby-forum.com/t/dir/93285/1 "2005-11-21T04:58:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 5:10am UTC](https://www.ruby-forum.com/t/dir/93285/2 "2005-11-21T05:10:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 5:14am UTC](https://www.ruby-forum.com/t/dir/93285/3 "2005-11-21T05:14:11Z")

</div>

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

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

c

---

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 5:30am UTC](https://www.ruby-forum.com/t/dir/93285/4 "2005-11-21T05:30:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 5:54am UTC](https://www.ruby-forum.com/t/dir/93285/5 "2005-11-21T05:54:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 10:40am UTC](https://www.ruby-forum.com/t/dir/93285/6 "2005-11-21T10:40:41Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![wes.monk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a587f6/32.png) [@wes.monk](https://www.ruby-forum.com/u/wes.monk)\
**Post date:** [November 21, 2005, 10:44am UTC](https://www.ruby-forum.com/t/dir/93285/7 "2005-11-21T10:44:45Z")

</div>

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.
