Skip to content

Instantly share code, notes, and snippets.

@EdWarga
Created July 6, 2020 17:58
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 EdWarga/1b6a55df92774e6bc86227a9427984c1 to your computer and use it in GitHub Desktop.
Save EdWarga/1b6a55df92774e6bc86227a9427984c1 to your computer and use it in GitHub Desktop.
# Read parent CSV
$InputFilename = Get-Content 'C:\working\splitCSV\testSplit.csv'
$OutputFilenamePattern = 'output_done_'
$LineLimit = 2000
# Initialize
$line = 0
$i = 0
$file = 0
$start = 0
# Loop all text lines
while ($line -le $InputFilename.Length) {
# Generate child CSVs
if ($i -eq $LineLimit -Or $line -eq $InputFilename.Length) {
$file++
$Filename = "$OutputFilenamePattern$file.csv"
$InputFilename[0] | Out-File $Filename -Force # Writes Header at the beginning of the line.
$InputFilename[$start..($line - 1)] | Out-File $Filename -Force -Append
# Original line 19 with the addition of -Append so it doesn't overwrite the headers you just wrote.
$start = $line;
$i = 0
Write-Host "$Filename"
}
# Increment counters
$i++;
$line++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment