Last active
October 5, 2021 13:14
-
-
Save mmilidoni/04dbf9a57f8168d6b658792ca508a57d to your computer and use it in GitHub Desktop.
Powershell script to split CSV files
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
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