Results 1 to 5 of 5

Thread: [RESOLVED] VB.NET Combine .txt files without header

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    Resolved [RESOLVED] VB.NET Combine .txt files without header

    Hi all

    I have the following code to combine all .txt files in a folder into a single .txt file :


    Code:
    Dim path As String = "D:\Wingerdbou\Lutzville2\InvoerDataFiles\Ander Organisasies\Wesbank\HistorieseFiles\"
            Dim topath As String = "D:\Wingerdbou\Lutzville2\InvoerDataFiles\Ander Organisasies\Wesbank\HistorieseFiles\"
            Dim files As String = "*.txt"
            Dim txtFiles() As String
            txtFiles = Directory.GetFiles(path, files)
            Using writer As New StreamWriter(topath & "\Combined.txt")
                For i As Integer = 0 To txtFiles.Length - 1
                    Using reader As StreamReader = File.OpenText(txtFiles(i))
                        writer.Write(reader.ReadToEnd())
    
                    End Using
                Next i
    
            End Using
    However in the process I also combine all the headers in the first line of all the files into the new .txt file.
    I just cannot figure out how to use only one column header. The data structure of the files are identical.


    Any help would be appreciated.

    Regards

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET Combine .txt files without header

    vb.net Code:
    1. Dim sourceFolderPath = "source folder path here"
    2. Dim outputFilPath = "output file path here"
    3.  
    4. Dim sourceFilePaths = Directory.GetFiles(sourceFolderPath, "*.txt")
    5.  
    6. Using writer As New StreamWriter(outputFilPath)
    7.     For i = 0 To sourceFilePaths.GetUpperBound(0)
    8.         Dim lines = File.ReadLines(sourceFilePaths(i))
    9.  
    10.         'Skip the first line if this is not the first file.
    11.         If i > 0 Then
    12.             lines = lines.Skip(1)
    13.         End If
    14.  
    15.         For Each line In lines
    16.             writer.WriteLine(line)
    17.         Next
    18.     Next
    19. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    Re: VB.NET Combine .txt files without header

    Thank you jmcilhinney.

    Works like a charm.
    However the new file in in Windows(CRLF) UTF8 format. The original files are in Unix(LF) UTF8 format.
    For importing it into MYSQL, I changed the carriage return to \r\n instead of \n (which works Unix(LF) files).
    I have tried both \r\n and \n , but it does not import with no error messages.
    Any Ideas?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET Combine .txt files without header

    You could change this:
    vb.net Code:
    1. writer.WriteLine(line)
    to this:
    vb.net Code:
    1. writer.Write(line & ControlChars.Lf)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    Re: VB.NET Combine .txt files without header

    Thank you very much.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width