# Help getting file names from directorys on windows

**URL:** <https://www.ruby-forum.com/t/help-getting-file-names-from-directorys-on-windows/158049>\
**Category:** Ruby\
**Created:** [January 18, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/help-getting-file-names-from-directorys-on-windows/158049 "2009-01-18T16:41:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [January 18, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/help-getting-file-names-from-directorys-on-windows/158049/1 "2009-01-18T16:41:20Z")

</div>

I am new to code writting, have to learn Ruby (windows) so I am trying  
to  
do some experaments working with files and directories.

This code, (I thinks) creates an array called file. I can do some tricks  
to test that, array.empty? and I get back false for each element. I want  
to be able to select each element on at a time But all I get is numbers.

Dir.chdir(“e:/sourcemp3”)  
puts Dir.pwd

Dir.foreach(".") do |file|  
puts file  
end

The code above will list all files in the directory. I need only the  
first  
file with .mp3 extion. It would be better not to use the .foreach but  
what  
could I use?

---

<div class="post-metadata">

**Author:** ![lostbit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a4c791/32.png) [@lostbit](https://www.ruby-forum.com/u/lostbit)\
**Post date:** [January 18, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/help-getting-file-names-from-directorys-on-windows/158049/2 "2009-01-18T17:34:12Z")

</div>

Harry N. wrote:

> I am new to code writting, have to learn Ruby (windows) so I am trying  
> to  
> do some experaments working with files and directories.
> 
> This code, (I thinks) creates an array called file. I can do some tricks  
> to test that, array.empty? and I get back false for each element. I want  
> to be able to select each element on at a time But all I get is numbers.
> 
> Dir.chdir(“e:/sourcemp3”)  
> puts Dir.pwd
> 
> Dir.foreach(“.”) do |file|  
> puts file  
> end
> 
> The code above will list all files in the directory. I need only the  
> first  
> file with .mp3 extion. It would be better not to use the .foreach but  
> what  
> could I use?

I usually use the Dir.glob() method in such instances:

puts Dir.glob(‘\*.mp3’).first

I hope that helps.

David

> **[Ruby on Windows](http://rubyonwindows.blogspot.com)**
>
> On using the Ruby programming language on the Microsoft Windows platform
