Ruby experts:
I know how to read each file in a subdirectory using the code below. My question is: How can this code be modified so that the code will, in addition, read each file in all subdirectories below “some_directory”?
(I think what I want is a way to read all subdirectories “recursively”.)
Thanks in advance!
Kurt Euler
Dir.new("./some_directory").each { |fname|
if (File.file? “./some_directory/”+fname)
File.open("./some_directory/"+fname) { |file|
do_stuff_here
}
end
}
(I think what I want is a way to read all subdirectories “recursively”.)
Dir[‘./some_directory/**/*’]
Chris
http://clabs.org
Hi,
I know how to read each file in a subdirectory using the code below. My question is: How can this code be modified so that the code will, in addition, read each file in all subdirectories below “some_directory”?
(I think what I want is a way to read all subdirectories “recursively”.)
Use zsh style recursive wildcard:
for f in Dir.glob(“./some_directory/**/*”)
File.open(f) { |file|
do_stuff_here
} if File.file?(f)
end
matz.
···
In message “How to read files in all subdirectories?” on 02/08/29, Kurt Euler keuler@portal.com writes:
Ruby experts:
I know how to read each file in a subdirectory using the code below. My
question is: How can this code be modified so that the code will, in
addition, read each file in all subdirectories below “some_directory”?
(I think what I want is a way to read all subdirectories “recursively”.)
Use the Find module, as per the following example.
Recurse into directories given on the command-line, printing the name of
each file, ignoring CVS directories, and adding / to directories.
require “find”
dirs = ARGV || [“.”]
Find.find(*dirs) do |path|
Find.prune if File.basename(path) == “CVS”
print path
print “/” if File.stat(path).directory?
print “\n”
end
Thanks in advance!
Kurt Euler
Gavin