title: Split large CSV file into multiple files based on the size with the parent file header
body: <p>I have a scenario where i have to split a large csv file in multiple csv files and each file should be 100 MB or less in size with the headers.    </p>
  
  <p>i have tried below VB.net code in my ssis package, but i am not getting the header row for child files.</p>
  
  <p>please help..</p>
  
  <pre class="lang-vb prettyprint-override"><code>
  Public Sub Main()
  
  
      Dim FileSize As Integer = 100000   'Specify In KB. Can Be Modified.
  
      Dim MasterPath As String = CStr(Dts.Variables("Filepath").Value) &amp; "\Master.Csv"
  
      Dim ChildPath As String = CStr(Dts.Variables("Filepath").Value) &amp; "\Child.Csv"
  
      Dim LogPath As String = CStr(Dts.Variables("Filepath").Value) &amp; "\Log.Txt"
  
      Try
  
          Call SplitFile(MasterPath, ChildPath, LogPath, FileSize)
  
      Catch Ex As Exception
  
          MsgBox(Ex.Message)
  
      End Try
  
      Dts.TaskResult = ScriptResults.Success
  
  End Sub
  Sub SplitFile(ByVal MasterPath As String, ByVal ChildPath As String, ByVal Logpath As String, ByVal SizeKB As Integer)
  
      Dim FilesizeCounter As Integer
  
      Dim FileCounter As Integer = 0
  
      Dim RowCount As Integer = 0
  
      'Open The Stream And Read It Back.
  
      Dim Parentsr As StreamReader = File.OpenText(MasterPath)
  
      Dim Childfs As FileStream
  
      Dim Logfs As FileStream
  
      Call CreateFile(Logpath, Logfs)  'Create Log File
  
      Do While Parentsr.Peek() &gt;= 0    'Looping Master File Stream
  
          If FilesizeCounter = 0 Then
  
              FileCounter = FileCounter + 1
  
              Call CreateFile(Replace(ChildPath, ".Csv", "_" &amp; FileCounter &amp; ".Csv"), Childfs)
  
          End If
  
          If FilesizeCounter &lt; (SizeKB * 1024) Then
  
              Call WriteFile(Childfs, Parentsr.ReadLine() &amp; vbNewLine, FilesizeCounter)
  
              If Parentsr.EndOfStream Then
  
                  Childfs.Close()
  
                  Call WriteFile(Logfs, "---------", 0)
  
                  Call WriteFile(Logfs, "File Name:" &amp; Replace(ChildPath, ".Csv", "_" &amp; FileCounter &amp; ".Csv") &amp; vbNewLine &amp; "Row Count:" &amp; RowCount &amp; vbNewLine &amp; "Size(Bytes):" &amp; FilesizeCounter &amp; vbNewLine &amp; "Extract End:" &amp; Now().ToString, 0)
  
              End If
  
              RowCount = RowCount + 1
  
          Else
  
              Call WriteFile(Childfs, Parentsr.ReadLine() &amp; vbNewLine, FilesizeCounter)
  
              Childfs.Close()   ' Close Child File
  
              Call WriteFile(Logfs, "---------", 0)
  
              Call WriteFile(Logfs, "File Name:" &amp; Replace(ChildPath, ".Csv", "_" &amp; FileCounter &amp; ".Csv") &amp; vbNewLine &amp; "Row Count:" &amp; RowCount &amp; vbNewLine &amp; "Size(Bytes):" &amp; FilesizeCounter &amp; vbNewLine &amp; "Extract End:" &amp; Now().ToString, 0)
  
              RowCount = RowCount + 1
  
              FilesizeCounter = 0    ' Reset File Size Counter
  
          End If
  
      Loop
  
      Parentsr.Close()  ' Close Master File
  
      Logfs.Close()     ' Close Log File
  
  End Sub
  
  Sub CreateFile(ByVal Path As String, ByRef Fs As FileStream)
  
      If File.Exists(Path) Then File.Delete(Path)  'Delete The If Already Exist.
  
      Fs = File.Create(Path)
  
  End Sub
  Sub WriteFile(ByRef Fs As FileStream, ByVal LineInfo As String, ByRef FilesizeCounter As Integer)
  
      Dim Info As Byte() = New Text.UTF8Encoding(True).GetBytes(LineInfo &amp; vbNewLine)
  
      Fs.Write(Info, 0, Info.Length)    ' Add Some Information To The File.
  
      FilesizeCounter = FilesizeCounter + Info.Length
  
  End Sub
  
  
  #Region "ScriptResults declaration"
  
  'This enum provides a convenient shorthand within the scope of this class for setting the
  'result of the script.
  
  'This code was generated automatically.
  Enum ScriptResults
      Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
      Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  End Enum
  
  #End Region
  
  End Class
  
  </code></pre>
  
  <p>i need to include master file headers into child files.</p>