Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / file

Merge Multiple text files in a folder

28 Jan 2011CPOL 28K  
How to merge all text files in a folder
This article will show you how to merge the text of text files located in a directory.
C#
string[] txtFiles;
txtFiles = Directory.GetFiles(txtFileDestination.Text, "*.txt");
using (StreamWriter writer = new StreamWriter(txtFileDestination.Text + @"\allfiles.txt"))
{
    for (int i = 0; i < txtFiles.Length; i++)
    {
        using (StreamReader reader = File.OpenText(txtFiles[i]))
        {
              writer.Write(reader.ReadToEnd());
        }
    }
}


[Modified: it really doesn't take much to fix the tabbing...]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)