Is there a way to use some type of foreach loop to read in each text file in a directory, each pass returning a file name and its contents in two separate strings, until all files have been read?
I believe there is a command that reads all file names in a directory into an array. However, I’d rather see if there’s a quick and dirty way to step through each file without creating (and then working with) such array.
Dir.new(‘dirname’).each { |fname|
if File.file? fname
File.open(fname) { |file|
contents = file.read
# do something with contents
}
end
}
···
On Tuesday 11 June 2002 10:08 pm, Kurt Euler wrote:
Is there a way to use some type of foreach loop to read in each
text file in a directory, each pass returning a file name and its
contents in two separate strings, until all files have been read?
Is there a way to use some type of foreach loop to read in each text
file in a directory, each pass returning a file name and its
contents in two separate strings, until all files have been read?
I believe there is a command that reads all file names in a
directory into an array. However, I’d rather see if there’s a quick
and dirty way to step through each file without creating (and then
working with) such array.
Is there a way to use some type of foreach loop to read in each text
file in a directory, each pass returning a file name and its
contents in two separate strings, until all files have been read?