Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Script to walk a directory tree

Reply
Thread Tools

Script to walk a directory tree

 
 
Kyle Heon
Guest
Posts: n/a
 
      07-25-2005
Can anyone point me in the direction of how to walk a directory tree
recursively?

I'm not having much luck finding code samples via Google.

Thanks.

Kyle Heon
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
 
Timothy Hunter
Guest
Posts: n/a
 
      07-25-2005
Kyle Heon wrote:
> Can anyone point me in the direction of how to walk a directory tree
> recursively?
>
> I'm not having much luck finding code samples via Google.
>
> Thanks.
>
> Kyle Heon
> (E-Mail Removed)
>
>
>


Check out 'find' in the standard library.

http://www.ruby-doc.org/stdlib/libdoc/find/rdoc/
 
Reply With Quote
 
 
 
 
Kirk Haines
Guest
Posts: n/a
 
      07-25-2005
On Monday 25 July 2005 5:22 pm, Kyle Heon wrote:
> Can anyone point me in the direction of how to walk a directory tree
> recursively?
>
> I'm not having much luck finding code samples via Google.


def file_search(dirpath, file_regex)
r = []
Dir.foreach(dirpath) do |filename|
next if filename == '.' or filename == '..'
fullname = "#{dirpath}/#{filename}"
if FileTest.directory? fullname
r.concat(file_search(fullname))
elsif file_regex.match(filename)
r.push fullname
end
end
r
end

That's quick and dirty, but should give you a starting place to develop what
you want. Pass into file_search a starting path (with a trailing '/'), and a
regex that will match what you are looking for. You'll get back and array
with everything that was found to match.


Kirk Haines


 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      07-25-2005

On 25 Jul 2005, at 16:22, Kyle Heon wrote:

> Can anyone point me in the direction of how to walk a directory tree
> recursively?
>
> I'm not having much luck finding code samples via Google.


require 'find'

Find.find('/your/path') do |f|
# if you want to skip all dirs
next if File.directory? f
puts "at: #{f}"
end

--
Eric Hodel - (E-Mail Removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04



 
Reply With Quote
 
Kirk Haines
Guest
Posts: n/a
 
      07-25-2005
On Monday 25 July 2005 5:33 pm, Eric Hodel wrote:
> On 25 Jul 2005, at 16:22, Kyle Heon wrote:
> > Can anyone point me in the direction of how to walk a directory tree
> > recursively?
> >
> > I'm not having much luck finding code samples via Google.

>
> require 'find'
>
> Find.find('/your/path') do |f|
> # if you want to skip all dirs
> next if File.directory? f
> puts "at: #{f}"
> end


Much better than implementing it manually, but the fun thing about manually is
that it's not that much longer. Love Ruby.


Kirk Haines


 
Reply With Quote
 
Ara.T.Howard
Guest
Posts: n/a
 
      07-26-2005
On Tue, 26 Jul 2005, Kirk Haines wrote:

> On Monday 25 July 2005 5:33 pm, Eric Hodel wrote:
>> On 25 Jul 2005, at 16:22, Kyle Heon wrote:
>>> Can anyone point me in the direction of how to walk a directory tree
>>> recursively?
>>>
>>> I'm not having much luck finding code samples via Google.

>>
>> require 'find'
>>
>> Find.find('/your/path') do |f|
>> # if you want to skip all dirs
>> next if File.directory? f
>> puts "at: #{f}"
>> end

>
> Much better than implementing it manually, but the fun thing about manually is
> that it's not that much longer. Love Ruby.


it gets alot longer if you handle links, devices, potential infinite recursion
due to directory links, and the fs changing underneath you causing errors to
be thrown as often happens in the case of gigantic or nfs fs's... speaking
from too much bad experience

btw. the find2 module on c.l.r offers a find that follows links - something
the built-in find does not do if this is important for the OP.

cheers.

-a
--
================================================== =============================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
================================================== =============================



 
Reply With Quote
 
nobuyoshi nakada
Guest
Posts: n/a
 
      07-26-2005
Hi,

At Tue, 26 Jul 2005 08:22:34 +0900,
Kyle Heon wrote in [ruby-talk:149485]:
> Can anyone point me in the direction of how to walk a directory tree
> recursively?


Dir.glob("**/*") do |fn|
# ...
end

--
Nobu Nakada


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os walk() and threads problems (os.walk are thread safe?) Marcus Alves Grando Python 7 11-14-2007 02:30 PM
DVD Verdict reviews: WALK THE LINE, JARHEAD, WALK THE LINE: TWO-DISC COLLECTOR'S EDITION, and more! DVD Verdict DVD Video 0 03-20-2006 09:19 AM
ntfw (new file tree walk) question ben.lemasurier@gmail.com C Programming 7 04-06-2005 10:54 AM
how to walk a binary tree pembed2003 C++ 7 04-20-2004 09:04 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments