Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > appending the contents of multiple text files into 1 file

Reply
Thread Tools

appending the contents of multiple text files into 1 file

 
 
Paul Danese
Guest
Posts: n/a
 
      06-14-2007
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


 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      06-14-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
newbie; appending multiple files Steve Perl Misc 5 05-07-2004 02:38 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM
Appending Text to File - Please Help!!! Tapanan Yeophantong ASP General 1 10-18-2003 06:58 AM
Load contents of a text file into a text box Jeremy Chapman ASP .Net 1 08-15-2003 10:10 PM



Advertisments