Prev: Speeding up TCP
Next: A regexp?
From: Hawksury Gear on 21 Mar 2010 15:24 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 21 Mar 2010 15:53 > 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 21 Mar 2010 15:54 > 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 22 Mar 2010 10:55 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 22 Mar 2010 11:19
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 |