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
kheon@comcast.net
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
kheon@comcast.net
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
kheon@comcast.net
Check out 'find' in the standard library.
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
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.
require 'find'
Find.find('/your/path') do |f|
# if you want to skip all dirs
next if File.directory? f
puts "at: #{f}"
end
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.
--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
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
Thanks everyone. All great samples. I'm playing around now.
Thanks again!
Kyle Heon
kheon@comcast.net
-----Original Message-----
From: Eric Hodel [mailto:drbrain@segment7.net]
Sent: Monday, July 25, 2005 7:33 PM
To: ruby-talk ML
Subject: Re: Script to walk a directory tree
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 - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
Much better than implementing it manually, but the fun thing about manually is
that it's not that much longer. Love Ruby.
Kirk Haines
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
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
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}"
endMuch better than implementing it manually, but the fun thing about manually is
that it's not that much longer. Love Ruby.
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
===============================================================================