Looking for a file somewhere in a directory recursively

R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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

..and doing some basic parsing on the results?
 
R

Robert Klemme

2010/8/23 Ralph Shnelvar said:
Assume I have a file pattern
=A0temp*.bat

Assume I have a directory d:\XXX

and that I want to find all instances using Windows' file matching mechan=
isms rather than than Ruby regular expressions or the functionality of fnma=
tch.
In other words ... it _has_ to be the Windows file matching method exactl=
y.

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 some= thing like

=A0X_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 =3D Dir['MyDir/*.bat']

or if you want to get fancy:

batches =3D Dir[File.join('MyDir', '*.bat')]

For the fully recursive version you can do

batches =3D Dir['MyDir/**/*.bat']

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

batches =3D Dir['MyDir/**/*.{bat,cmd}']

and really get all Windows batch files.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

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.
 
R

Rob Biedenharn

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.


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 Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
R

Robert Klemme

2010/8/23 Ralph Shnelvar said:
Robert,

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

RK> For the pattern above you only need

RK> batches =3D Dir['MyDir/*.bat']

RK> or if you want to get fancy:

RK> batches =3D Dir[File.join('MyDir', '*.bat')]

RK> For the fully recursive version you can do

RK> batches =3D Dir['MyDir/**/*.bat']

RK> and be done. =A0No Gems required. =A0You can even do

RK> batches =3D Dir['MyDir/**/*.{bat,cmd}']

RK> and really get all Windows batch files.

Is the stuff, above, some sort of 1.9 syntax? =A0Do you really mean squar=
e 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top