# Simple get file by filename

**URL:** <https://www.ruby-forum.com/t/simple-get-file-by-filename/113890>\
**Category:** Ruby\
**Created:** [September 5, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/simple-get-file-by-filename/113890 "2007-09-05T22:56:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 5, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/simple-get-file-by-filename/113890/1 "2007-09-05T22:56:13Z")

</div>

Consider this:

I have a list of files that I get by calling:

Dir.entries("/")

I just want to search this list and match the file name that matches  
1\_1.\* ( i.e. the filename without the extension)

So, for example, If the pattern to match against the list was  
“jpg\_image.” it should return the filename “jpg\_image.jpg” (assuming  
the file exists).

What’s the most succinct way to do this in ruby?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 5, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/simple-get-file-by-filename/113890/2 "2007-09-05T23:01:16Z")

</div>

On 9/5/07, eggie5 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “jpg\_image.” it should return the filename “jpg\_image.jpg” (assuming  
> the file exists).
> 
> What’s the most succinct way to do this in ruby?

Dir[“/jpg\_image.\*”]

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 5, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/simple-get-file-by-filename/113890/3 "2007-09-05T23:16:17Z")

</div>

On Sep 5, 2:00 pm, “Gregory B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > So, for example, If the pattern to match against the list was  
> > “jpg\_image.” it should return the filename “jpg\_image.jpg” (assuming  
> > the file exists).
> 
> > What’s the most succinct way to do this in ruby?
> 
> Dir[“/jpg\_image.\*”]

Confirmed.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/simple-get-file-by-filename/113890/4 "2007-09-25T23:04:51Z")

</div>

eggie5 wrote:

> What’s the most succinct way to do this in ruby?

Dir[‘1\_1._’]  
Dir['jpg\_image._’]

ri Dir.glob

Regards,
