3

I want to merge all of the tab-delimited text files in a directory into one giant text file. The files don't have headers and the columns in all of the files are all aligned properly with each other so let's assume we don't have to worry about formatting consistency issues.

I just have to stitch/join/merge all of the files together in no particular order.

Here's my code that works:

    string[] array = Directory.GetFiles(@"C:\MergeThis", "*.txt");

    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    for (int nCount = 0; nCount <= array.Count(); nCount++)
    {
        sb.Append(System.IO.File.ReadAllText(array[nCount])); 
    }

    string output = sb.ToString();    

    string outputFilePath = @"C:\MERGED DATA.txt";
    System.IO.File.WriteAllText(outputFilePath, output);

My question is..is there a better/faster/more concise way of doing this?

MrPatterns
  • 4,184
  • 27
  • 65
  • 85
  • looks fine to me :o maybe this is off topic in SO and you should put it on code review – MichaC Oct 05 '13 at 15:16
  • How big are the text files? You might consider using `File.ReadLines` for large files because it can be more efficient than `File.ReadAllLines`. See MSDN. – User 12345678 Oct 05 '13 at 15:17
  • You definitely don't have to read everything in memory at once. Open file -> write to merged output -> repeat. Should use just a token amount of memory as a read buffer, regardless of the size of the input. – Jon Oct 05 '13 at 15:18
  • maybe this is the answer - http://stackoverflow.com/questions/6311358/efficient-way-to-combine-multiple-text-files – terrybozzio Oct 05 '13 at 15:45

1 Answers1

6

Dont know if thats faster than yours but here is a solution provided by n8wrl in their answer here:

using (var output = File.Create("output"))
{
    foreach (var file in new[] { "file1", "file2" })
    {
        using (var input = File.OpenRead(file))
        {
            input.CopyTo(output);
        }
    }
}

Note:- Stream.CopyTo Method is a feature of .Net 4.0

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    If there's no line terminator for the last line in "file1", the "output" file will have the last line from "file1" concatenated with the first line from "file2"; Depending on how the output is used I'd either track the last few bytes from the previous file OR simply add a line terminator between files. – Cosmin Prund Oct 05 '13 at 15:20