Script to walk a directory tree

K

Kyle Heon

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 address removed)
 
K

Kirk Haines

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
 
E

Eric Hodel

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
 
K

Kirk Haines

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
 
A

Ara.T.Howard

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
===============================================================================
 
N

nobuyoshi nakada

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top