From: Hawksury Gear on
Hello,
I am new to Ruby.

I am trying to "Read Content" of all the files from a Directory. So far
I can "Open" all files but this just shows the "names of all files" in
the directory and not the "file content".
My Program code is
arr= Dir.open("K:/test").entries
arr.each { |i| puts i }

I can open and read the content of an individual file using "File.open"
command but I don't know how to read the content of all the files in a
particular Directory.

It is may be very simple but I just can't find a way to do. Could you
please help?
Many Thanks,
:)
--
Posted via http://www.ruby-forum.com/.

From: Jonathan Nielsen on
> I am trying to "Read Content" of all the files from a Directory. So far
> I can "Open" all files but this just shows the "names of all files" in
> the directory and not the "file content".
> My Program code is
>  arr= Dir.open("K:/test").entries
>  arr.each { |i| puts i }
>
> I can open and read the content of an individual file using "File.open"
> command but I don't know how to read the content of all the files in a
> particular Directory.
>

Hi,

You've got a good start there. As you have already figured out, the
arr.each iterator there gives you the name of each file. So, what you
would want to do to read the content of each file, is inside the each
iterator do a File.open(i), then do whatever you need with the
contents of the file.

arr = Dir.ope("K:/test").entires
arr.each do |file|
File.open(file) do |fd|
# do whatever you need with the file
end
end


-Jonathan Nielsen

From: Jonathan Nielsen on
> arr = Dir.open("K:/test").entries
> arr.each do |file|
>  File.open(file) do |fd|
>    # do whatever you need with the file
>  end
> end
>

Wow, I typoed that bad, but I hope you get the idea. (corrected above.)

From: Robert Klemme on
2010/3/21 Jonathan Nielsen <jonathan(a)jmnet.us>:
>> arr = Dir.open("K:/test").entries
>> arr.each do |file|
>>  File.open(file) do |fd|
>>    # do whatever you need with the file
>>  end
>> end
>>
>
> Wow, I typoed that bad, but I hope you get the idea.  (corrected above.)

If it is only for output purposes, we can actually do it in one line:

puts Dir["K:/test/*"].map {|f| File.read f}

Note: this is not necessarily efficient nor safe (just think of 3GB files..).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: jbw on
Also might want to check if it is a file and skip directories:

puts Dir["/*"].map { |f| if(!File.directory?(f)) then File.read f end }

But yes this isn't necessarily efficient or safe.

On Mon, Mar 22, 2010 at 2:55 PM, Robert Klemme
<shortcutter(a)googlemail.com> wrote:
> 2010/3/21 Jonathan Nielsen <jonathan(a)jmnet.us>:
>>> arr = Dir.open("K:/test").entries
>>> arr.each do |file|
>>>  File.open(file) do |fd|
>>>    # do whatever you need with the file
>>>  end
>>> end
>>>
>>
>> Wow, I typoed that bad, but I hope you get the idea.  (corrected above.)
>
> If it is only for output purposes, we can actually do it in one line:
>
> puts Dir["K:/test/*"].map {|f| File.read f}
>
> Note: this is not necessarily efficient nor safe (just think of 3GB files...).
>
> Kind regards
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>
>



--
jbw

 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Speeding up TCP
Next: A regexp?