Skip to content

Instantly share code, notes, and snippets.

@rytmis
Created April 20, 2018 10:09
Show Gist options
  • Save rytmis/35e64dd24ecb009cf3d0320a13333d3b to your computer and use it in GitHub Desktop.
Save rytmis/35e64dd24ecb009cf3d0320a13333d3b to your computer and use it in GitHub Desktop.
function Read-Chunk {
param([System.IO.StreamReader] $streamReader, [int] $chunkSize)
$i = 0;
while (($i -lt $chunkSize -and ($line = $streamReader.ReadLine()))) {
$i++;
Write-Output $line
}
}
function Get-ChunkedContent {
param($File, $ChunkSize)
try {
$stream = [System.IO.File]::OpenRead($File)
$streamReader = New-Object "System.IO.StreamReader" -ArgumentList $stream
$chunkNumber = 1
$chunk = $null
while ($chunk = (Read-Chunk $streamReader $ChunkSize)) {
Write-Output @{
Number = $chunkNumber;
Content = $chunk;
}
$chunkNumber++
}
} finally {
$streamReader.Dispose()
}
}
Import-Module FileManagement.psm1
Get-ChunkedContent "filename" 5000 | %{ $_.Content > "File$($_.Number).txt" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment