|
|
Title | Chop a large file of tab-delimited data into manageable pieces in VB .NET |
Description | This example shows how to chop a large file into manageable pieces in VB .NET. |
Keywords | ADO, database, insert, quotes |
Categories | VB.NET, Files and Directories, Software Engineering |
|
|
Thanks to Carl.
The key is the CFileChopper class's CreateChoppedFiles method. It opens the input file with a StreamReader, reads the lines in the file, and writes them into the smaller files.
|
|
Public Function CreateChoppedFiles() As String
Dim FileNamesCreated = ""
Dim i As Integer
Dim PartialOutputFileName As String
PartialOutputFileName = _
Path.GetDirectoryName(Me.m_FileNameToConvert) & "\" _
& Path.GetFileNameWithoutExtension(Me.m_FileNameToConvert)
For i = 1 To m_NumberOfFilesToCreate
m_sr = CreateStreamReader(Me.m_FileNameToConvert)
Dim mincolumnnumber As Integer = 2 + ((i - 1) * _
(MAXFIELDSPEROUTPUTFILE - 1))
Dim maxcolumnnumber As Integer = mincolumnnumber + _
MAXFIELDSPEROUTPUTFILE - 2
If maxcolumnnumber > m_Columnnames.Length Then
maxcolumnnumber = m_Columnnames.Length
End If
'MessageBox.Show(mincolumnnumber & " - " &
' maxcolumnnumber)
Dim newfilename As String = PartialOutputFileName & _
Chr(65 + i - 1) & _
Path.GetExtension(Me.m_FileNameToConvert)
Dim sw As New StreamWriter(newfilename, False)
Dim nextline As String
nextline = m_sr.ReadLine()
Do Until nextline = "" Or IsNothing(nextline)
Dim DataFieldData() As String = _
nextline.Split(ControlChars.Tab)
sw.Write(DataFieldData(0))
Dim col As Integer
For col = mincolumnnumber - 1 To _
maxcolumnnumber - 1
sw.Write(ControlChars.Tab & _
DataFieldData(col))
Next
sw.Write(ControlChars.CrLf)
nextline = m_sr.ReadLine()
Loop
m_sr.Close()
sw.Close()
If FileNamesCreated = "" Then
FileNamesCreated = newfilename
Else
FileNamesCreated = FileNamesCreated & "; " & _
ControlChars.CrLf & newfilename
End If
Next
Return FileNamesCreated
End Function
|
|
|
|
|
|