Search code examples
vb.netopenfiledialogsavefiledialog

Merging 2 or more text files after edit in VB.net


Helo there!

it seems that i am facing a problem with my code in VB.net. Please be patient as i am a complete beginner in programming. I am trying to code a program that will load 2 or more txt files, find and exclude specific lines (starting with some characters or contain some characters) and then merge and save only one file that will contain all the information after the editing (from all the files).

I am using openfiledialog and i have set the multiselect to true. Below is the code for the OpenfileDialog:

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        For Each File In OpenFileDialog1.FileNames
            My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
        Next

If i am correct, it loads the filenames and reads all the text from the files. For the editing i am using the following code:

        Dim outputLines As New List(Of String)()
    For Each line As String In System.IO.File.ReadLines(OpenFileDialog1.FileName)
        Uline1 = line.StartsWith("text1")
        Uline2 = line.StartsWith("text2")
        Uline3 = line.StartsWith("text3")
        Uline4 = line.StartsWith("text4")
        Uline5 = line.StartsWith("text5")
        Uline6 = line.StartsWith("text7")
        Uline7 = line.StartsWith("sometext")
        Trash = line.Contains("^")

        If Uline1 Or Uline2 Or Uline3 Or Uline4 Or Uline5 Or Uline6 Or Uline7 Or Trash Then
            outputLines.Remove(line)
        Else
            outputLines.Add(line)
        End If
    Next

For the output i am using a savefiledialog with the following code:

    SaveFileDialog1.DefaultExt = "txt"
    SaveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    SaveFileDialog1.RestoreDirectory = True
    If (SaveFileDialog1.ShowDialog() = DialogResult.OK) Then
        System.IO.File.WriteAllLines(SaveFileDialog1.FileName, outputLines)

Although the files are being loaded correctly, the edit seems to happen only in one file (the last selected) and again the program saves only one file.

Could you please point me to the right direction?


Solution

  • You need to nest your loops through the file names returned by the open file dialog, and the lines returned by the ReadLines calls. You also don't need to remove lines from the outputLines list, since they are never added. Something like:

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim outputLines As New List(Of String)()
        For Each fileName In OpenFileDialog1.FileNames
            For Each line As String In System.IO.File.ReadLines(fileName)
                Uline1 = line.StartsWith("text1")
                Uline2 = line.StartsWith("text2")
                Uline3 = line.StartsWith("text3")
                Uline4 = line.StartsWith("text4")
                Uline5 = line.StartsWith("text5")
                Uline6 = line.StartsWith("text7")
                Uline7 = line.StartsWith("sometext")
                Trash = line.Contains("^")
                If Not (Uline1 Or Uline2 Or Uline3 Or Uline4 Or Uline5 Or Uline6 Or Uline7 Or Trash) Then
                    outputLines.Add(line)
                End If
            Next
        Next
    End If
    

    If the files are very large you will start to run into memory issues, and will need to write the data out as it is read instead of keeping it all in memory.

    UPDATE

    Based on the comments, if you want to check the following line to determine if the current line should be written, you could use something like this. Note the use of ReadAllLines and the For loop.

    Dim outputLines As New List(Of String)()
    For Each fileName In OpenFileDialog1.FileNames
        Dim lines() As String = System.IO.File.ReadAllLines(fileName)
        For i As Integer = 0 To lines.Count - 1
            Dim line As String = lines(i)
            If line.StartsWith("19") AndAlso i < lines.Count - 2 AndAlso lines(i + 1).StartsWith("15") Then
                outputLines.Add(line)
                outputLines.Add(lines(i + 1))
                i += 1
            End If
        Next
    Next