# Looking for a file somewhere in a directory recursively

**URL:** <https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730>\
**Category:** Ruby\
**Created:** [August 23, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730 "2010-08-23T16:50:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730/1 "2010-08-23T16:50:03Z")

</div>

Assume I have a file pattern  
temp\*.bat

Assume I have a directory d:\XXX

and that I want to find all instances using Windows’ file matching  
mechanisms rather than than Ruby regular expressions or the  
functionality of fnmatch.

In other words … it _has_ to be the Windows file matching method  
exactly.

I see that FileUtils has a bunch of functionality but what I want is  
something like

X\_FileUtils.dir(“MyDir”).pattern("\*.bat") { |file| puts file }

Is there a gem “out there” to do what I want?

Ralph

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 5:04pm UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730/2 "2010-08-23T17:04:41Z")

</div>

What about doing something like this:  
result = %x[dir \*.bat]

…and doing some basic parsing on the results?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730/3 "2010-08-23T17:29:17Z")

</div>

2010/8/23 Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Assume I have a file pattern  
> temp\*.bat
> 
> Assume I have a directory d:\XXX
> 
> and that I want to find all instances using Windows’ file matching mechanisms rather than than Ruby regular expressions or the functionality of fnmatch.
> 
> In other words … it _has_ to be the Windows file matching method exactly.

What exactly do you mean by “Windows file matching method”? Windows  
uses globbing very similar to most Unix shells. So as long as you  
stick with using “\*” and “?” you can just use Dir.glob or Dir.[].

> I see that FileUtils has a bunch of functionality but what I want is something like
> 
> X\_FileUtils.dir(“MyDir”).pattern(“\*.bat”) { |file| puts file }
> 
> Is there a gem “out there” to do what I want?

For the pattern above you only need

batches = Dir[‘MyDir/\*.bat’]

or if you want to get fancy:

batches = Dir[File.join(‘MyDir’, ‘\*.bat’)]

For the fully recursive version you can do

batches = Dir[‘MyDir/\*\*/\*.bat’]

and be done. No Gems required. You can even do

batches = Dir[‘MyDir/\*\*/\*.{bat,cmd}’]

and really get all Windows batch files.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730/4 "2010-08-23T19:19:23Z")

</div>

On Aug 23, 2010, at 11:47 AM, Ralph S. wrote:

> RK\> and be done. No Gems required. You can even do  
> RK\> batches = Dir[‘MyDir/\*\*/\*.{bat,cmd}’]  
> RK\> and really get all Windows batch files.
> 
> Is the stuff, above, some sort of 1.9 syntax? Do you really mean  
> square brackets?
> 
> On 1.8 … I cannot get anything you wrote, about, to work.

Different Rob(ert), but yes, there is a [] method defined on Dir

There’s no reason to think that it wouldn’t work on your Windows  
system. Mine is a Mac, but the ruby code is the same:

irb\> RUBY\_VERSION  
=\> “1.8.6”  
irb\> Dir[‘_.rb’]  
=\> [“listdiff.rb”]  
irb\> Dir['code/ruby/mailing\_list/\*\*/_.rb’]  
=\> [“code/ruby/mailing\_list/assoc.rb”, “code/ruby/mailing\_list/  
merge\_csv.rb”, “code/ruby/mailing\_list/sales\_data\_graph.rb”, “code/  
ruby/mailing\_list/simulating\_vehicles.rb”, “code/ruby/mailing\_list/  
sorting\_with\_numbers.rb”]

Note that _I_ have a code/ruby/mailing\_list/ directory, but you’d have  
to use directory names that exist on your system.

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 23, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730/5 "2010-08-23T17:48:37Z")

</div>

Robert,

Monday, August 23, 2010, 9:25:56 AM, you wrote:

RK\> For the pattern above you only need

RK\> batches = Dir[‘MyDir/\*.bat’]

RK\> or if you want to get fancy:

RK\> batches = Dir[File.join(‘MyDir’, ‘\*.bat’)]

RK\> For the fully recursive version you can do

RK\> batches = Dir[‘MyDir/\*\*/\*.bat’]

RK\> and be done. No Gems required. You can even do

RK\> batches = Dir[‘MyDir/\*\*/\*.{bat,cmd}’]

RK\> and really get all Windows batch files.

Is the stuff, above, some sort of 1.9 syntax? Do you really mean square  
brackets?

On 1.8 … I cannot get anything you wrote, about, to work.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [August 24, 2010, 9:13am UTC](https://www.ruby-forum.com/t/looking-for-a-file-somewhere-in-a-directory-recursively/193730/6 "2010-08-24T09:13:40Z")

</div>

2010/8/23 Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> RK\> batches = Dir[File.join(‘MyDir’, ‘\*.bat’)]
> 
> Is the stuff, above, some sort of 1.9 syntax? Do you really mean square brackets?

No. Yes.

> On 1.8 … I cannot get anything you wrote, about, to work.

Please show your IRB session / code and the errors you got.

Kind regards

robert
