# Appending the contents of multiple text files into 1 file

**URL:** <https://www.ruby-forum.com/t/appending-the-contents-of-multiple-text-files-into-1-file/106851>\
**Category:** Ruby\
**Created:** [June 14, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/appending-the-contents-of-multiple-text-files-into-1-file/106851 "2007-06-14T17:20:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [June 14, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/appending-the-contents-of-multiple-text-files-into-1-file/106851/1 "2007-06-14T17:20:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dangermoose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@dangermoose](https://www.ruby-forum.com/u/dangermoose)\
**Post date:** [June 14, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/appending-the-contents-of-multiple-text-files-into-1-file/106851/2 "2007-06-14T18:27:08Z")

</div>

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
