Last active
June 14, 2016 12:28
-
-
Save vermorel/9a6ca39b3102b6453322 to your computer and use it in GitHub Desktop.
Split large CSV files into smaller files while preserving the headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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