Click here to Skip to main content
16,016,076 members
Articles / Programming Languages / Visual Basic
Article

VB.Net Text File Splitter

Rate me:
Please Sign up or sign in to vote.
2.05/5 (11 votes)
8 Apr 2005 70.2K   3K   20   5
This simple project shows how to use the Stream Reader and Stream Writer to read and write files

Source Files: Download source code - 37.4 Kb

Screen Shot
Sample image


 

I was working on a much larger project when I realized I needed to make a huge text file smaller so I could load it into notepad easier, and still retain the the main format of the file.

I couldn't use a file splitter because it splits the file based on File size, and they tend to split the file in the middle of lines, and not at the end of the line.

So I went about writing a tiny application that will take a text file and split the file line by line until the number of lines in the new file is the size the user wants.

I have just made a slight modification 4/8/2005 that now allows the user to choose if they wish to split the file up by a certain number of lines or a cetain number of files. If the user chooses to split the file by a certain number of files the program will create the number of files with equal amouts of lines

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have been developing software since 1995, and I enjoy it very much. My current position allows me to keep up with the latest technology, and it is refreshing.


I enjoy Woodworking, Fishing, Camping, and Hunting. I live on the west coast of Florida and enjoy spending my free time outdoors, or building stuff in my workshop.

Comments and Discussions

 
GeneralI needed a really fast solution and this one seems to do it. Pin
Doug Hill10-Jul-14 8:24
Doug Hill10-Jul-14 8:24 
I needed a really fast, split by number of lines solution and this one seems to do it. Wrote this in VS2013. Note - I think File.Readlines is a dotnet 4.5 method.

VB
Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
		btnGo.Enabled = False
		lstOutput.Items.Clear()
		Dim sb As New StringBuilder(300000)
		Dim Suffix As Integer = 1
		Dim MaxLines As Integer = CInt(txtLineNum.Text)
		Dim outName As String
		Dim i As Integer = 0

		outName = ShowProgress(Suffix)
		For Each line As String In File.ReadLines(txtFilename.Text)
			sb.AppendLine(line)
			i += 1
			If i = MaxLines Then
				i = 0
				Using op As New StreamWriter(outName, False)
					op.Write(sb.ToString())
					op.Flush()
					op.Close()
				End Using
				Suffix += 1
				sb.Length = 0
				outName = ShowProgress(Suffix)
			End If
		Next
		If sb.Length > 0 Then
			Using op As New StreamWriter(outName, False)
				op.Write(sb.ToString())
				op.Flush()
				op.Close()
			End Using
		End If

		sb = Nothing
		btnGo.Enabled = True
		MsgBox("Done", MsgBoxStyle.Exclamation)
	End Sub
	Private Function ShowProgress(ByVal fileNum As Integer) As String
		Dim oName As String = txtFilename.Text & fileNum.ToString("000") & ".txt"
		lstOutput.Items.Add(oName)
		lstOutput.Refresh()
		Application.DoEvents()
		Return oName
	End Function

Questionspeeding it up Pin
rudyman3-Aug-11 5:11
rudyman3-Aug-11 5:11 
GeneralCInt and Rounding Pin
pccat11-Apr-06 6:03
pccat11-Apr-06 6:03 
QuestionProblem with CInt function? Pin
pccat10-Apr-06 14:24
pccat10-Apr-06 14:24 
AnswerRe: Problem with CInt function? Pin
Luca Crisi, MCP4-Mar-10 5:38
Luca Crisi, MCP4-Mar-10 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.