Click here to Skip to main content
15,885,546 members
Articles / General Programming / File
Tip/Trick

Merge Multiple text files in a folder

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
28 Jan 2011CPOL 27.5K   3   2
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)


Written By
Software Developer Cybosoft Technologies
Australia Australia
I am working as a Team Leader in HimanshuSofttech(http://www.himanshusofttech.com) a new born Search Engine Optimization and software developement Company Located in Dewas, A Holy place of India. I started my programing life as a VB.NET Developer but now I am working in C#.NET.

Comments and Discussions

 
QuestionMerging multiple text file data in to one text file Pin
SN252-May-18 20:15
SN252-May-18 20:15 
QuestionWhat is name of destination file here to which all files are copied? Pin
Member 1349591431-Oct-17 4:20
Member 1349591431-Oct-17 4:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.