C# Merging Two or more Text Files side by side

Adam Strobel
using (StreamWriter writer = File.CreateText(FinishedFile))
{
    int lineNum = 0;
    while (lineNum < FilesLineCount.Min())
    {
        for (int i = 0; i <= FilesToMerge.Count() - 1; i++)
        {
            if (i != FilesToMerge.Count() - 1)
            {
                var CurrentFile = File.ReadLines(FilesToMerge[i]).Skip(lineNum).Take(1);
                string CurrentLine = string.Join("", CurrentFile);
                writer.Write(CurrentLine + ",");
            }
            else
            {
                var CurrentFile = File.ReadLines(FilesToMerge[i]).Skip(lineNum).Take(1);
                string CurrentLine = string.Join("", CurrentFile);
                writer.Write(CurrentLine + "\n");
            }
        }
        lineNum++;
    }
}

The current way i am doing this is just too slow. I am merging files that are each 50k+ lines long with various amounts of data.

for ex: File 1
1
2
3
4

File 2
4
3
2
1

i need this to merge into being a third file
File 3
1,4
2,3
3,2
4,1

P.S. The user can pick as many files as they want from any locations.
Thanks for the help.

Tim Schmelter

You approach is slow because of the Skip and Take in the loops.

You could use a dictionary to collect all line-index' lines:

string[] allFileLocationsToMerge = { "filepath1", "filepath2", "..." };
var mergedLists = new Dictionary<int, List<string>>();
foreach (string file in allFileLocationsToMerge)
{
    string[] allLines = File.ReadAllLines(file);
    for (int lineIndex = 0; lineIndex < allLines.Length; lineIndex++)
    {
        bool indexKnown = mergedLists.TryGetValue(lineIndex, out List<string> allLinesAtIndex);
        if (!indexKnown)
            allLinesAtIndex = new List<string>();
        allLinesAtIndex.Add(allLines[lineIndex]);
        mergedLists[lineIndex] = allLinesAtIndex;
    }
}

IEnumerable<string> mergeLines = mergedLists.Values.Select(list => string.Join(",", list));
File.WriteAllLines("targetPath", mergeLines);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related