Results 1 to 4 of 4

Thread: [RESOLVED] Split Text File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Resolved [RESOLVED] Split Text File

    Hi I have a text file which is about 700MB it is very difficult to open this file
    but there are 5800 Chapters in the file every chapter is starting from "$$NEW$$" For example

    Code:
    "$$NEW$$"
    Some Text
    "$$NEW$$"
    Some Text
    "$$NEW$$"
    Some Text
    "$$NEW$$"
    Some Text
    I want to split the text file after every chapter ends any idea ?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Split Text File

    try this:

    vb Code:
    1. Dim contents As String = IO.File.ReadAllText(filename)
    2. Dim chapters() As String = contents.Split(New String() {"""$$NEW$$"""}, StringSplitOptions.RemoveEmptyEntries)
    3. For Each c In chapters
    4.     MsgBox(c.Trim)
    5. Next

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Split Text File

    Using IO.File.ReadAllText with a 700MB file is not recommended. Instead, you use a streamreader and create a new output file every time you read a line that has "$$NEW$$" in it. Something like this:
    vb.net Code:
    1. Dim input As String = "full path to your input text file here"
    2.         Using reader As New System.IO.StreamReader(input)
    3.             Dim line As String = String.Empty
    4.             Dim chapter As Integer = 1
    5.             Dim outfile As String = String.Empty
    6.             Dim sb As New System.Text.StringBuilder()
    7.             While reader.Peek >= 0
    8.                 line = reader.ReadLine()
    9.                 If line.Contains("""$$NEW$$""") Then
    10.                     If sb.Length > 0 Then
    11.                         outfile = System.IO.Path.GetDirectoryName(input) & "\Chapter_" & chapter.ToString & ".txt"
    12.                         System.IO.File.WriteAllText(outfile, sb.ToString)
    13.                         chapter += 1
    14.                         sb.Length = 0
    15.                     End If
    16.                 End If
    17.                 sb.AppendLine(line)
    18.             End While
    19.             outfile = System.IO.Path.GetDirectoryName(input) & "\Chapter_" & chapter.ToString & ".txt"
    20.             System.IO.File.WriteAllText(outfile, sb.ToString)
    21.         End Using
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    99

    Re: Split Text File

    Thank you very much guys
    Code:
    stanav
    you get out me from a very big trouble Thanks

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