appending the contents of multiple text files into 1 file

Discussion in 'Ruby' started by Paul Danese, Jun 14, 2007.

  1. Paul Danese

    Paul Danese Guest

    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
    Paul Danese, Jun 14, 2007
    #1
    1. Advertising

  2. 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
    Stefano Crocco, Jun 14, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Chapman

    Load contents of a text file into a text box

    Jeremy Chapman, Aug 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    749
    Tommy
    Aug 15, 2003
  2. Replies:
    4
    Views:
    938
    M.E.Farmer
    Feb 13, 2005
  3. Tapanan Yeophantong

    Appending Text to File - Please Help!!!

    Tapanan Yeophantong, Oct 18, 2003, in forum: ASP General
    Replies:
    1
    Views:
    137
    Ray at
    Oct 18, 2003
  4. Kamarulnizam Rahim
    Replies:
    4
    Views:
    204
    Robert Klemme
    Jan 28, 2011
  5. Steve

    newbie; appending multiple files

    Steve, May 6, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    135
    David K. Wall
    May 7, 2004
Loading...

Share This Page