appending the contents of multiple text files into 1 file

P

Paul Danese

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 =3D File.new('C:\mynewfile.txt', 'w')
@myfilenames.each do |mfn|
File.open('#{mfn}.txt') do |file|
file.each_line {|line| mynewfile.puts(line)}
end =20
end
mynewfile.close
 
S

Stefano Crocco

Alle gioved=EC 14 giugno 2007, Paul Danese ha scritto:
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 =3D 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

This should work:

=46ile.open('C:\mynewfile.txt','w') do |f|
@myfilenames.each do |mfn|
f.puts(File.read(mfn))
end
end

I hope this helps

Stefano
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top