Skip to content

Instantly share code, notes, and snippets.

@mmilidoni
Last active October 5, 2021 13:14
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 mmilidoni/04dbf9a57f8168d6b658792ca508a57d to your computer and use it in GitHub Desktop.
Save mmilidoni/04dbf9a57f8168d6b658792ca508a57d to your computer and use it in GitHub Desktop.
Powershell script to split CSV files
param ($source, $lines)
if ($source -eq $null) {
$source = read-host -Prompt "Please enter a filename"
}
if ($lines -eq $null) {
$lines = read-host -Prompt "Please enter the number of lines for each chunk"
}
# variable used to store the path of the source CSV file
$sourceCSV = $source;
$InputFilename = Get-Content $sourceCSV
$outname = (Get-Item $sourceCSV).Basename;
$startrow = 0 ;
$counter = 1 ;
while ($startrow -lt $InputFilename.Length) {
Import-CSV $sourceCSV | select-object -skip $startrow -first $lines | Export-CSV "$outname chunk$($counter).csv" -NoClobber -notype;
$startrow += $lines;
$counter++ ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment