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.txt")
.Copy("file1.txt", 10)
.Copy("file2.txt", 10)
.Copy("file3.txt", 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(file, IO.FileMode.Open, IO.FileAccess.Read)
sr = New IO.StreamReader(fs, System.Text.Encoding.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(file,
IO.FileMode.CreateNew, IO.FileAccess.Write)
Dim sw As IO.StreamWriter = New IO.StreamWriter(fs,
System.Text.Encoding.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(input)
End If
End While
sw.Close()
End Sub
End Class
<iv********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.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