Click here to Skip to main content
12,551,373 members (55,384 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked

Windows Application for Merging Text Files

, 3 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Merging text files using the StreamReader class and OpenFileDialog/OpenFolderDialog for selecting files

Screenshot - FileMerge.gif


This application helps you merge two text files into one. You can either select individual files or select a folder to merge all of the text files in it. The File / Folder dialog box helps to select files.

Using the code

Text files can be merged using basic file operations. Using the StreamReader class, files from the list box (LstFiles) can be opened, read to a temporary string (temp) and appended to a new file. The new file name is typed into SaveFileDialog.

Private Sub SaveFileDialog1_FileOk(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles SaveFileDialog1.FileOk

    Dim FileReader As StreamReader
    Dim i As Integer = 0
    Dim temp As String

    For i = 0 To LstFiles.Items.Count - 1
        FileReader = File.OpenText(LstFiles.Items.Item(i))
        temp = FileReader.ReadToEnd
        File.AppendAllText(SaveFileDialog1.FileName, temp)

    Dim prompt As String
    prompt = String.Concat(lbl_NoOfFiles.Text, " files merged succesfully")
    MsgBox(prompt, MsgBoxStyle.Information, "Merge")
    lbl_NoOfFiles.Text = "0"

End Sub

All files in a folder can be selected by clicking on the Select Folder button. Filenames are stored in the files() array by first using FolderBrowserDialog and then adding the items of the array to the list box.

Private Sub BtnFolderMerge_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles BtnFolderMerge.Click

    Dim files() As String
    files = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.txt")
    Dim i As Integer = 0

    For i = 0 To files.Length - 1

    Label2.Visible = True
    lbl_NoOfFiles.Text = CStr(LstFiles.Items.Count)
    lbl_NoOfFiles.Visible = True

End Sub

Points of interest

To filter the File dialog, a filter like this is helpful. This displays only files with the *.txt extension: OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"


  • Article first submitted on July 30th, 2007


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


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralYou are the man (or woman)! Pin
Frank Field30-Jun-09 6:32
memberFrank Field30-Jun-09 6:32 
GeneralI appreciate this code Pin
chunchun20056-Nov-07 11:47
memberchunchun20056-Nov-07 11:47 
GeneralVery helpful - Thanks Pin
smoore44-Oct-07 9:54
membersmoore44-Oct-07 9:54 
GeneralStupid thing. Pin
AlejandroDG3-Aug-07 18:08
memberAlejandroDG3-Aug-07 18:08 
GeneralRe: Stupid thing. Pin
mav.northwind4-Aug-07 23:13
membermav.northwind4-Aug-07 23:13 
GeneralRe: Stupid thing. Pin
nikunj9155-Aug-07 8:52
membernikunj9155-Aug-07 8:52 
GeneralRe: Stupid thing. Pin
galberry16-Aug-07 15:43
membergalberry16-Aug-07 15:43 
Generalthank you Pin
nikunj91521-Aug-07 21:10
membernikunj91521-Aug-07 21:10 
GeneralRe: Stupid thing. Pin
wakalah21-Aug-07 0:32
memberwakalah21-Aug-07 0:32 
GeneralRe: Stupid thing. Pin
smoore44-Oct-07 9:41
membersmoore44-Oct-07 9:41 
GeneralRe: Stupid thing. Pin
lazygenius27-Aug-08 5:13
memberlazygenius27-Aug-08 5:13 
GeneralRe: Stupid thing. [modified] Pin
lamp15930-Sep-08 13:55
memberlamp15930-Sep-08 13:55 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 3 Aug 2007
Article Copyright 2007 by nikunj915
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid