Results 1 to 4 of 4

Thread: Split line from big text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Split line from big text file

    I need to split line from one big text file.(80MB)

    For example, text file look like below

    ST*837*000008~BHT*0019*00*1*20140205*1020*RP~REF*87*004010X098~

    I need code to update it as below based on "~" letter.

    ST*837*000008~
    BHT*0019*00*1*20140205*1020*RP~
    REF*87*004010X098~

    How to do it?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Split line from big text file

    Are you talking about reading a text file that is a single line, then splitting up that text file by the ~ character and storing the splits as new lines in the same file? If so look at this example:
    Code:
    Option Strict On
    Option Explicit On
    Module Module1
    
        Sub Main()
            Dim t As Threading.Thread = New Threading.Thread(Sub() SplitFile("my_file_here.txt", "~"c))
            t.Start()
            Console.ReadLine()
        End Sub
    
        Private Sub SplitFile(ByVal path As String, ByVal delimiter As Char)
            Console.WriteLine("Reading file.")
            Dim contents As String = My.Computer.FileSystem.ReadAllText(path)
            Dim newContents As String = String.Empty
    
            Console.WriteLine("Splitting contents by the " & "~" & " character.")
            For Each line As String In contents.Split({delimiter}, StringSplitOptions.RemoveEmptyEntries)
                newContents &= line & Environment.NewLine
            Next
    
            My.Computer.FileSystem.WriteAllText(path, newContents, False)
            Console.WriteLine("Finished editting the file.")
        End Sub
    
    End Module
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: Split line from big text file

    Thank you. It works great!

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Split line from big text file

    There is one typo I made, on this line:
    Code:
    Console.WriteLine("Splitting contents by the " & "~" & " character.")
    It should be this:
    Code:
    Console.WriteLine("Splitting contents by the " & delimiter.ToString & " character.")
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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