Hi All,

I am currently creating a merging program which is processing multiple text files with header. I've manage to merge the text files but I'm stucked with the header that needs computation.

path of input files and header explanation: Input Files
After merging, I need to put back the header, adding up each values, which I explained in the path above. How can I do that?

Here's my current code:

Imports System.IO
Public Class Form1
    Dim openFolder As New FolderBrowserDialog
    'The routines must be 
    '1) Merge the txt file with the same version number number say all version 20, 23 and 13. 
    '2) Clean up the merged txt file by removing the unnecessary headers and adjusting number order form for merged txt file. 
    Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click
        Try
            openFolder.ShowDialog()
            Dim dir As New DirectoryInfo(openFolder.SelectedPath)
            Dim textFiles As FileInfo() = dir.GetFiles("*.txt")
            Dim textFile As FileInfo
            For Each textFile In textFiles
                'System.IO.File.AppendAllText(openFolder.SelectedPath & "\merged.txt", System.IO.File.ReadAllText(openFolder.SelectedPath & "\" & textFile.ToString))
                Call mergeV20(openFolder.SelectedPath & "\" & textFile.ToString)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
        MessageBox.Show("Done!")
    End Sub
    Private Sub mergeV20(ByVal filepath As String)
        Try
            'reader declaration
            Dim reader As StreamReader = New StreamReader(filepath)
            Dim writer As New StreamWriter(openFolder.SelectedPath & "\merged.txt", True)
            'get filename from full path
            Dim intPos As Integer
            intPos = filepath.LastIndexOfAny("\")
            intPos += 1
            Dim fileName As String = filepath.Substring(intPos, (Len(filepath) - intPos))
            Dim fileFpath As String = filepath.Substring(0, intPos)
            Dim fileCount As Integer = Directory.GetFiles(fileFpath, "*.txt").Length
            'end get filename from full path
            'start of merging function
            'start header part
            reader.ReadLine()
            While Not reader.EndOfStream
                Dim oLines As String = reader.ReadLine()
                writer.WriteLine(oLines, True)
            End While
            'end header part
            writer.Close()
            writer.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

Thanks in advance.

Please post the Input Files info here. When I try to follow your link my anti-virus software says

Opening this website may put your security at risk

I just get "Incorrect linking code."

Don't make me jump through hoops. Just post the data here.

Comments
">>Don't make me jump through hoops...". :)

Nope. It just gets stuck at "Processing download request".

The text messes up when directly posting here.

If that's the best you can do when describing an error we're not going to get anything fixed here. Are you aware that you can click on USE ADVANCED EDITOR where you can attach (for example) zip files to your posts?

Edited 5 Years Ago by Reverend Jim: n/a

In your example, the three separate headers are

0010115112011                              00015[B]00065[/B]00020
0010115112011                              00011[B]00054[/B]00020
0010115112011                              00002[B]00010[/B]00020

So wouldn't the combined header just be the sums? In this case

0010115112011                              00028[B]00129[/B]00020

Look on the page for

Has this thread been answered?
If this thread has been successfully resolved, please Mark this Thread as Solved so that it can be added to our Knowledge Base and help others. Also, posters who replied appreciate knowing that they were helpful.

I mean, what's the code for combining the header?

Edited 5 Years Ago by renzlo: n/a

The following code contains a function which will split the header into four fields. In the example, the results are:

0010115112011
00015
00065
00020

Using that you can convert the counts to Integer using CInt, then once you get the totals after the merge you can rebuild the combined header.

Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button.Click

    Dim header As String = "0010115112011                              000150006500020"
    Dim fields() As String = SplitHeader(header)

End Sub

Private Function SplitHeader(header As String) As String()

    Dim temp() As String = header.Split()
    Dim fields(3) As String
    Dim field2 As Integer = UBound(temp)

    fields(0) = temp(0)
    fields(1) = temp(field2).Substring(0, 5)
    fields(2) = temp(field2).Substring(5, 5)
    fields(3) = temp(field2).Substring(10)

    Return fields

End Function
This question has already been answered. Start a new discussion instead.