473,748 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split large text file by number of lines?

Hello,

im a beginner in VB.NET... The thing i would like to do is as it
follows....

I have a text file (list of names, every name to the next line) which
is about 350000 lines long. I would like to split it and create a new
file at every lets say 20000 lines... so, the directory output would
have to be something like this:

File1: 1-20000 lines of the original file
File2: 20001-40000 lines of the original file
File3: 40001-60000 lines of the original file

etc.

Can it be done simply? one form with field to enter the number of
lines, button to load a text file and a "Start" button...

Thanks in advance

Feb 21 '07 #1
6 24156
Yes.

Read the source file line by line

Write each line to the target file

After each nth line, close the target file and open a new one (with a
different name of course).
<iv********@gma il.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
Hello,

im a beginner in VB.NET... The thing i would like to do is as it
follows....

I have a text file (list of names, every name to the next line) which
is about 350000 lines long. I would like to split it and create a new
file at every lets say 20000 lines... so, the directory output would
have to be something like this:

File1: 1-20000 lines of the original file
File2: 20001-40000 lines of the original file
File3: 40001-60000 lines of the original file

etc.

Can it be done simply? one form with field to enter the number of
lines, button to load a text file and a "Start" button...

Thanks in advance
Feb 21 '07 #2
This code I have writen works but it takes some time to complete(about 50
seconds for a 1 mb text file)

Mabye beter to do a "readall" and then use the SPLIT(str, vbcrlf) function
anyway this should do it

add a textbox and a button. This is created in vb.net 2005 (the free
version from microsoft)

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

TextSplitter()

End Sub
Sub TextSplitter()

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

Dim AsciiStreamRead er As IO.StreamReader =
IO.File.OpenTex t("C:\HugeSourc eTextFile1.txt" )

Dim sb As New Text.StringBuil der

Dim LineCounter As Integer = 0

Dim FileNumber As Integer = 1

Dim bProcessWinMsg As Boolean = 0

Me.Text = "processng file... "

While AsciiStreamRead er.EndOfStream = False

bProcessWinMsg += 1

If bProcessWinMsg Then Application.DoE vents()

sb.Append(Ascii StreamReader.Re adLine() & vbCrLf)

If LineCounter = CInt(TextBox1.T ext) Or AsciiStreamRead er.EndOfStream = True
Then

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

IO.File.WriteAl lText("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

"Stephany Young" <noone@localhos twrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Yes.

Read the source file line by line

Write each line to the target file

After each nth line, close the target file and open a new one (with a
different name of course).
<iv********@gma il.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
>Hello,

im a beginner in VB.NET... The thing i would like to do is as it
follows....

I have a text file (list of names, every name to the next line) which
is about 350000 lines long. I would like to split it and create a new
file at every lets say 20000 lines... so, the directory output would
have to be something like this:

File1: 1-20000 lines of the original file
File2: 20001-40000 lines of the original file
File3: 40001-60000 lines of the original file

etc.

Can it be done simply? one form with field to enter the number of
lines, button to load a text file and a "Start" button...

Thanks in advance

Feb 22 '07 #3
"Michael M." <no****@mike.co mschrieb
This code I have writen works but it takes some time to
complete(about 50 seconds for a 1 mb text file)

Mabye beter to do a "readall" and then use the SPLIT(str, vbcrlf)
function anyway this should do it

add a textbox and a button. This is created in vb.net 2005 (the
free version from microsoft)

Suggestion (untested):

Sub TextSplitter()

Dim fsIN, fsOut As IO.FileStream
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim OutCount As Integer

fsIN = New IO.FileStream( _
"infile.txt ", IO.FileMode.Ope n, IO.FileAccess.R ead _
)

sr = New IO.StreamReader (fsIN, System.Text.Enc oding.Default)

Do
Dim Line As String
Dim LineCount As Integer

Line = sr.ReadLine()
If Line Is Nothing Then Exit Do

If fsOut Is Nothing Then
OutCount += 1

fsOut = New IO.FileStream( _
"outfile" & OutCount & ".txt", _
IO.FileMode.Cre ateNew, IO.FileAccess.W rite _
)

sw = New IO.StreamWriter (fsOut, System.Text.Enc oding.Default)
LineCount = 0
End If

sw.WriteLine(Li ne)
LineCount += 1

If LineCount = 20000 Then
sw.Close()
fsOut = Nothing
End If
Loop

If fsOut IsNot Nothing Then
sw.Close()
End If

fsIN.Close()

End Sub
Be aware that Encoding.Ascii supports only 7 bit characters.
Armin

Feb 22 '07 #4
I'm going to opt for an OOP solution which isn't quite so dependent upon all
the inputs being fixed. Personally I've learned that "specs change" and
planning for change saves the client money which makes for a happy client.

So try the other solutions out and then try this one. Do note that you can
change the input file name, set the output file names, set the line count
(indpendently per file) and it can produce more (or fewer) than 3 files by
calling the Copy() method as many times as you want.

Personally I'd add some error handling before I tried to sell it and I might
add a methodology to indicate the end of the input file was reached. While
it should cause no harm it seems pointless to keep calling Copy() if the
end-of-file was already reached.

The reason there is only 30 lines indicated in my example is that's all I
wanted to type into my test.

Tom

Dim oCopier As Copier = New Copier()

With oCopier
.Open("infile.t xt")
.Copy("file1.tx t", 10)
.Copy("file2.tx t", 10)
.Copy("file3.tx t", 10)
.Close()
End With
Public Class Copier
Inherits Object

Private fs As IO.FileStream
Private sr As IO.StreamReader

Public Sub Open(ByVal file As String)
fs = New IO.FileStream(f ile, IO.FileMode.Ope n, IO.FileAccess.R ead)
sr = New IO.StreamReader (fs, System.Text.Enc oding.Default)
End Sub

Public Sub Close()
sr.Close()
End Sub

Public Sub Copy(ByVal file As String, ByVal max As Int32)

Dim fs As IO.FileStream = New IO.FileStream(f ile,
IO.FileMode.Cre ateNew, IO.FileAccess.W rite)
Dim sw As IO.StreamWriter = New IO.StreamWriter (fs,
System.Text.Enc oding.Default)

Dim input As String
Dim count As Int32 = 0

Dim processing As Boolean = True
While processing

input = sr.ReadLine()
count += 1

processing = ((input IsNot Nothing) AndAlso count < max)

If processing Then
sw.WriteLine(in put)
End If

End While

sw.Close()

End Sub

End Class


<iv********@gma il.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
Hello,

im a beginner in VB.NET... The thing i would like to do is as it
follows....

I have a text file (list of names, every name to the next line) which
is about 350000 lines long. I would like to split it and create a new
file at every lets say 20000 lines... so, the directory output would
have to be something like this:

File1: 1-20000 lines of the original file
File2: 20001-40000 lines of the original file
File3: 40001-60000 lines of the original file

etc.

Can it be done simply? one form with field to enter the number of
lines, button to load a text file and a "Start" button...

Thanks in advance

Feb 22 '07 #5
you know they make partitioning in databases right?

keep everything in one table and then you can use rank; or you can
filter, search-- anything you want to do.

and it doesn't matter if you have 20k records or 4m...

I mean seriously; why reinvent the wheel? Did I mention why reinvent
the wheel?

On Feb 21, 1:58 pm, ivan.pe...@gmai l.com wrote:
Hello,

im a beginner in VB.NET... The thing i would like to do is as it
follows....

I have a text file (list of names, every name to the next line) which
is about 350000 lines long. I would like to split it and create a new
file at every lets say 20000 lines... so, the directory output would
have to be something like this:

File1: 1-20000 lines of the original file
File2: 20001-40000 lines of the original file
File3: 40001-60000 lines of the original file

etc.

Can it be done simply? one form with field to enter the number of
lines, button to load a text file and a "Start" button...

Thanks in advance

Feb 22 '07 #6

Armin Zingler je napisao/la:
"Michael M." <no****@mike.co mschrieb
This code I have writen works but it takes some time to
complete(about 50 seconds for a 1 mb text file)

Mabye beter to do a "readall" and then use the SPLIT(str, vbcrlf)
function anyway this should do it

add a textbox and a button. This is created in vb.net 2005 (the
free version from microsoft)


Suggestion (untested):

Sub TextSplitter()

Dim fsIN, fsOut As IO.FileStream
Dim sr As IO.StreamReader
Dim sw As IO.StreamWriter
Dim OutCount As Integer

fsIN = New IO.FileStream( _
"infile.txt ", IO.FileMode.Ope n, IO.FileAccess.R ead _
)

sr = New IO.StreamReader (fsIN, System.Text.Enc oding.Default)

Do
Dim Line As String
Dim LineCount As Integer

Line = sr.ReadLine()
If Line Is Nothing Then Exit Do

If fsOut Is Nothing Then
OutCount += 1

fsOut = New IO.FileStream( _
"outfile" & OutCount & ".txt", _
IO.FileMode.Cre ateNew, IO.FileAccess.W rite _
)

sw = New IO.StreamWriter (fsOut, System.Text.Enc oding.Default)
LineCount = 0
End If

sw.WriteLine(Li ne)
LineCount += 1

If LineCount = 20000 Then
sw.Close()
fsOut = Nothing
End If
Loop

If fsOut IsNot Nothing Then
sw.Close()
End If

fsIN.Close()

End Sub
Be aware that Encoding.Ascii supports only 7 bit characters.
Armin
thanks man, this code does exactly what i need, and pretty fast....

Mar 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2150
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received e-mail. The content of the e-mail is actually predictable, it has one very long list of numbers, something looking like this:
12
5684
by: Martin Dieringer | last post by:
I am trying to split a file by a fixed string. The file is too large to just read it into a string and split this. I could probably use a lexer but there maybe anything more simple? thanks m.
2
12838
by: SL_McManus | last post by:
Hi All; I am fairly new to Perl. I have a file with close to 3000 lines that I would like to split out in a certain way. I would like to put the record type starting in column 1 for 2 spaces, the employer code in column 23 for 29 spaces and employer description in column 53 for 30 spaces. I have tried modifying an existing file with no real success. I haven't found anything that specifically answers my question. Any guidance would be...
3
4421
by: rxl124 | last post by:
Hi, room Beginner of learning perl here!! I have question to all, I have below file name datebook.master which contains only 2 lines Mike wolf:12/3/44:144 park ave, paramus: 44000 Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000 Now, I am testing out below scripts and working fine.
3
9670
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3... field1...field2...field3...
2
3610
by: Curious Joe | last post by:
I have some files that are anywhere from 3GB to 9GB and I need to split them down to a series of smaller files similar to what the "split" command in linux can do. Unfortunately, I do not have access to a linux machine right now. I have been told that a program could be written in C/C++ that would do this very quickly. Can anyone point me to a tutorial or how-to that will teach me to write a quick program do accomplish this? for...
24
4850
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable however. Assuming i've read the contents of the file into the string myfile, how do i split my...
20
4282
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
2
1668
by: ogo796 | last post by:
Hi guys am having a problem with a split(),i retrieve line from the text file and i wanna split that line.i manage to split two words but splitting the string enclosed on brackets it seems to be a problem to me. can someone look at my code and please help me maybe i am missing something. <?php $q=$_POST; $jaar=$_POST; $day=$_POST; //echo $day."<br />";
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.