Looking for a file somewhere in a directory recursively

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

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

…and doing some basic parsing on the results?

2010/8/23 Ralph S. [email protected]:

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

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.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

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.

2010/8/23 Ralph S. [email protected]:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs