Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   appending the contents of multiple text files into 1 file (http://www.velocityreviews.com/forums/t841560-appending-the-contents-of-multiple-text-files-into-1-file.html)

Paul Danese 06-14-2007 03:19 PM

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 =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



Stefano Crocco 06-14-2007 04:25 PM

Re: appending the contents of multiple text files into 1 file
 
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



All times are GMT. The time now is 04:50 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57