Click here to Skip to main content
15,890,185 members
Articles / Programming Languages / Visual Basic
Article

Windows Application for Merging Text Files

Rate me:
Please Sign up or sign in to vote.
1.96/5 (8 votes)
3 Aug 2007CPOL 51.7K   1.4K   23   12
Merging text files using the StreamReader class and OpenFileDialog/OpenFolderDialog for selecting files

Screenshot - FileMerge.gif

Introduction

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.

VB
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)
    Next

    Dim prompt As String
    prompt = String.Concat(lbl_NoOfFiles.Text, " files merged succesfully")
    MsgBox(prompt, MsgBoxStyle.Information, "Merge")
    LstFiles.Items.Clear()
    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.

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

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

    For i = 0 To files.Length - 1
        LstFiles.Items.Add(files(i))
    Next

    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"

History

  • Article first submitted on July 30th, 2007

License

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralYou are the man (or woman)! Pin
Frank Field30-Jun-09 6:32
Frank Field30-Jun-09 6:32 
GeneralI appreciate this code Pin
chunchun20056-Nov-07 11:47
chunchun20056-Nov-07 11:47 
GeneralVery helpful - Thanks Pin
smoore44-Oct-07 9:54
smoore44-Oct-07 9:54 
GeneralStupid thing. Pin
AlejandroDG3-Aug-07 18:08
AlejandroDG3-Aug-07 18:08 
GeneralRe: Stupid thing. Pin
mav.northwind4-Aug-07 23:13
mav.northwind4-Aug-07 23:13 
GeneralRe: Stupid thing. Pin
nikunj9155-Aug-07 8:52
nikunj9155-Aug-07 8:52 
GeneralRe: Stupid thing. Pin
galberry16-Aug-07 15:43
galberry16-Aug-07 15:43 
Generalthank you Pin
nikunj91521-Aug-07 21:10
nikunj91521-Aug-07 21:10 
GeneralRe: Stupid thing. Pin
wakalah21-Aug-07 0:32
wakalah21-Aug-07 0:32 
GeneralRe: Stupid thing. Pin
smoore44-Oct-07 9:41
smoore44-Oct-07 9:41 
GeneralRe: Stupid thing. Pin
lazygenius27-Aug-08 5:13
lazygenius27-Aug-08 5:13 
GeneralRe: Stupid thing. [modified] Pin
lamp15930-Sep-08 13:55
lamp15930-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.