Skip to content

Instantly share code, notes, and snippets.

@vermorel
Last active June 14, 2016 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vermorel/9a6ca39b3102b6453322 to your computer and use it in GitHub Desktop.
Save vermorel/9a6ca39b3102b6453322 to your computer and use it in GitHub Desktop.
Split large CSV files into smaller files while preserving the headers
# Splitting large CSV files into smaller ones, preserving the headers
# By Joannes Vermorel, 2015-12-18
function Split-Csv
{
param
(
[string] $file,
[int] $maxLines
)
$isFirst = $true;
$lineCount = 0;
if($file.StartsWith(".\"))
{
$file = (Get-Item -Path $file -Verbose).FullName;
}
$path = [System.IO.Path]::GetDirectoryName($file);
$output = [System.IO.Path]::GetFileNameWithoutExtension($file);
$outext = [System.IO.Path]::GetExtension($file);
$outputCount = 0;
$stream = [System.IO.StreamWriter] ($path + "\\" + $output + "-" + $outputCount + $outext);
foreach ($line in [System.IO.File]::ReadLines($file))
{
if($isFirst)
{
$header = $line;
$stream.WriteLine($header);
$lineCount = 1;
$isFirst = $false;
}
else
{
if($lineCount -eq 0)
{
$stream = [System.IO.StreamWriter] ($path + "\\" + $output + "-" + $outputCount + $outext);
$stream.WriteLine($header);
$stream.WriteLine($line);
$lineCount = 1;
}
else
{
$stream.WriteLine($line);
$lineCount = $lineCount + 1;
if($lineCount -gt $maxLines)
{
$stream.Flush();
$stream.Close();
$lineCount = 0;
$outputCount = $outputCount + 1;
}
}
}
}
$stream.Flush();
$stream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment