Appending the contents of multiple text files into 1 file

Hi,

is there a simpler/more idiomatic way to append/join the contents of
multiple text files into 1 file?

this works, but i’m trying to see if there are more succinct methods.
thanks!

mynewfile = File.new(‘C:\mynewfile.txt’, ‘w’)
@myfilenames.each do |mfn|
File.open(’#{mfn}.txt’) do |file|
file.each_line {|line| mynewfile.puts(line)}
end
end
mynewfile.close

Alle giovedì 14 giugno 2007, Paul D. ha scritto:

File.open('#{mfn}.txt') do |file|
  file.each_line {|line| mynewfile.puts(line)}
end

end
mynewfile.close

This should work:

File.open(‘C:\mynewfile.txt’,‘w’) do |f|
@myfilenames.each do |mfn|
f.puts(File.read(mfn))
end
end

I hope this helps

Stefano

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs