Click here to Skip to main content
Licence CPOL
First Posted 3 Aug 2007
Views 24,719
Downloads 325
Bookmarked 21 times

Windows Application for Merging Text Files

By | 3 Aug 2007 | Article
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.

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.

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)

About the Author

nikunj915

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYou are the man (or woman)! PinmemberFrank Field6:32 30 Jun '09  
GeneralI appreciate this code Pinmemberchunchun200511:47 6 Nov '07  
GeneralVery helpful - Thanks Pinmembersmoore49:54 4 Oct '07  
GeneralStupid thing. PinmemberAlejandroDG18:08 3 Aug '07  
GeneralRe: Stupid thing. Pinmembermav.northwind23:13 4 Aug '07  
GeneralRe: Stupid thing. Pinmembernikunj9158:52 5 Aug '07  
GeneralRe: Stupid thing. Pinmembergalberry15:43 16 Aug '07  
Generalthank you Pinmembernikunj91521:10 21 Aug '07  
GeneralRe: Stupid thing. Pinmemberwakalah0:32 21 Aug '07  
GeneralRe: Stupid thing. Pinmembersmoore49:41 4 Oct '07  
GeneralRe: Stupid thing. Pinmemberlazygenius5:13 27 Aug '08  
GeneralRe: Stupid thing. [modified] Pinmemberlamp15913:55 30 Sep '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 3 Aug 2007
Article Copyright 2007 by nikunj915
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid