# Find.find, limited recursion

**URL:** <https://www.ruby-forum.com/t/find-find-limited-recursion/175576>\
**Category:** Ruby\
**Created:** [September 15, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576 "2009-09-15T23:02:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 15, 2009, 11:02pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/1 "2009-09-15T23:02:43Z")

</div>

Hi all,

I’m working on a script that’s currently using Find.find to process a  
complete directory tree of files and directories … .however I needed  
specific behavior, and I’m still fairly new to this…

Basically it works like this, the user specifies the root directory of a  
collection of sub-directories we are interested in … for instance:

User specifies: C:\Root

C:\Root  
|  
–\>\Dir1  
|  
–\>Files\_in\_dir1  
|  
–\>\Dir2  
|  
–\>Files\_in\_dir2

But we are only interested in the _files_ in \Dir1 , \Dir2 …and want  
to  
essentially Prune recursion into C:\Root\Dir1\Subdir\_of\_Dir1 , and  
C:\Root\Dir2\Subdir\_of\_Dir2 if those exist …

This is what I am currently using, however it will pick up the sub  
directories of Dir1 and Dir2 and update the hash… Does anyone see any  
way  
this could be refactored to not update the “process\_list” hash with the  
sub  
directories of Dir1 and Dir2?

–

@root\_dir = “C:\Root”

process\_list = Hash.new {|h,k| h[k] = []}

Find.find(@root\_dir.to\_s) do |f|  
f = f.gsub(/\/,’/’)  
next if File.stat(f).directory? or f.include?(‘skipped\_filename’)  
Find.prune if f.include?(“skipped\_dirname”)  
d, b = File.split(f)  
process\_list [d] \<\< b  
end

Thanks in advance!

Brian

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 3:48am UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/2 "2009-09-16T03:48:58Z")

</div>

Brian W. wrote:

> end
> 
> Thanks in advance!
> 
> Brian

Perhaps you would like file-find better. I think this is what you want:

gem install file-find

rule = File::Find.new(  
:path =\> [‘C:/Root/Dir1’, ‘C:/Root/Dir2’],  
:maxdepth =\> 2,  
:directory? =\> false  
)

p rule.find

[http://shards.rubyforge.org/wiki/wiki.pl?File-Find](http://shards.rubyforge.org/wiki/wiki.pl?File-Find)

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 8:52am UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/3 "2009-09-16T08:52:43Z")

</div>

2009/9/15 Brian W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> Thanks in advance!

require ‘no gems’ # silly joke

Dir[“#{@root\_dir}/_/_”].each do |f|  
next unless File.file? f  
d, b = File.split f  
process\_list[d] \<\< b  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/4 "2009-09-16T15:44:48Z")

</div>

Thanks Dan,

That would actually work out OK - however since i’m using JRuby I  
wouldn’t  
be able to use a gem with native C extensions…

Thanks for the suggestion!

Brian

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/5 "2009-09-16T15:52:25Z")

</div>

Robert,

Hrrm - A simple solution of course … and I’ve spent all this time  
trying  
to figure it out on my own , without asking for help! 🙂

You’ve helped me in the past with a few other problems, and you were  
spot on  
each time …

I envy you, and hope that I am able to become even as half as  
knowledgeable  
as you are…

Best Regards,

Brian

On Wed, Sep 16, 2009 at 2:51 AM, Robert K.

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/6 "2009-09-16T19:10:54Z")

</div>

Hi,

Am Mittwoch, 16. Sep 2009, 06:01:48 +0900 schrieb Brian W.:

> end  
> Of course I recommend my RbFind tool. It is pure Ruby, no C to  
> compile.

[http://raa.ruby-lang.org/project/rbfind](http://raa.ruby-lang.org/project/rbfind)

Do something like (untested):

process\_list = Hash.new { |h,k| h[k] = [] }

RbFind.run root do  
prune if dir?  
next if name =~ /skipped/  
process\_list[dirname] \<\< name  
end

Bertram

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/7 "2009-09-16T17:41:54Z")

</div>

On Sep 16, 7:43 am, Brian W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Dan,
> 
> That would actually work out OK - however since i’m using JRuby I wouldn’t  
> be able to use a gem with native C extensions…
> 
> Thanks for the suggestion!

There’s a JRuby specific gem for file-find. A simple “gem install file-  
find” should work, but if it doesn’t try “gem install file-find –  
platform=java” and see if that works.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![brianwisti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@brianwisti](https://www.ruby-forum.com/u/brianwisti)\
**Post date:** [September 16, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/find-find-limited-recursion/175576/8 "2009-09-16T21:55:59Z")

</div>

On 16.09.2009 15:51, Brian W. wrote:

> Hrrm - A simple solution of course … and I’ve spent all this time trying  
> to figure it out on my own , without asking for help! 🙂

🙂

> You’ve helped me in the past with a few other problems, and you were spot on  
> each time …

Great to hear that I could provide helpful insights.

> I envy you, and hope that I am able to become even as half as knowledgeable  
> as you are…

You sure will - you practically cannot avoid it: experience takes time -  
and it automatically comes with time. 🙂

Btw, it’s the mistakes we learn from.

Kind regards

robert
