vb.net - text split

Hello,
I have a text file which is about 100 MB in size.. it contains records each line by line with ids like this

984098498498
0656898409804
9804908498
8098498980985

I would like to split this file into multiple files but since its so big my notepad its not responding for the most part..

I'm looking for a way to split it into multiple parts based on 20,000 lines..
how can I do this?
LVL 1
XK8ERAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
sure, this is possible.
for some sample code to start with:
http://bytes.com/topic/visual-basic-net/answers/605945-split-large-text-file-number-lines

the idea is to read the file, split by line, and by looping/counting the numbers of lines for output, you write into new files.
0
lluddenCommented:
There are plenty of cheap or free utilities out there that do just that.

http://www.gdgsoft.com/gsplit/

One of the options it has is just what you are looking for.
0
HaiFaiCommented:
Hi

Add openfiledialog and button to form and add this code

Imports System.IO
Public Class Form1
    Dim splitcount As Integer = 0
    Dim fcount As Integer = 0
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        Dim sr As New StreamReader(OpenFileDialog1.FileName)
        Dim sw As StreamWriter = New StreamWriter(OpenFileDialog1.FileName & ".part" & fcount, False)
        Do Until sr.EndOfStream
            sw.WriteLine(sr.ReadLine)
            splitcount += 1
            If splitcount = 20000 Then
                sw.Close()
                fcount += 1
                sw = New StreamWriter(OpenFileDialog1.FileName & ".part" & fcount, False)
                splitcount = 0
            End If
        Loop
        sw.Close()
    End Sub
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QuinnDexCommented:
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. please follow this example it's very useful.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextSplitter()

End Sub
Sub TextSplitter()

' open the source fle and read assign it to a stream

Dim AsciiStreamReader As IO.StreamReader =
IO.File.OpenText("C:\HugeSourceTextFile1.txt")

Dim sb As New Text.StringBuilder

Dim LineCounter As Integer = 0

Dim FileNumber As Integer = 1

Dim bProcessWinMsg As Boolean = 0

Me.Text = "processng file... "

While AsciiStreamReader.EndOfStream = False

bProcessWinMsg += 1

If bProcessWinMsg Then Application.DoEvents()

sb.Append(AsciiStreamReader.ReadLine() & vbCrLf)

If LineCounter = CInt(TextBox1.Text) Or AsciiStreamReader.EndOfStream = True
Then

' Writes the data stored in the stringBuiler(sb) and then closes the file

IO.File.WriteAllText("C:\" & "File " & FileNumber & ".txt", sb.ToString,
Encoding.ASCII)

' Reset the line count, clear the sb string and increment the file number

LineCounter = 0

sb.Length = 0

FileNumber += 1

End If

LineCounter += 1

End While

Me.Text = "Complete: created " & FileNumber & " files"

End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.