Skip to content

Instantly share code, notes, and snippets.

@peetrike
Last active April 7, 2021 20:55
Show Gist options
  • Save peetrike/0e96462b5e227057a33a4aa1ec31d213 to your computer and use it in GitHub Desktop.
Save peetrike/0e96462b5e227057a33a4aa1ec31d213 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Splits .CSV file into parts.
#>
param (
[parameter(
Mandatory = $true
)]
[string]
# File name to split
$FileName,
[parameter(
Mandatory = $true
)]
[Int64]
# Number of lines in one part.
$Lines,
[int]
# Number of Header line. The lines before are not used. Setting it to 0 means no header in splitted parts
$Head = 1
)
$file = Get-Item $FileName
$part = 0
if ($Head) {
$Header = Get-Content $file.FullName -Head $Head | Select-Object -Last 1
}
Get-Content $file.FullName -ReadCount $Lines | ForEach-Object {
$part++
$newName = Join-Path -Path $file.DirectoryName -ChildPath ($file.BaseName + $part + $file.Extension)
if ($Head -and ($part -gt 1)) {
Set-Content -Path $newName -Value $Header
}
Write-Verbose -Message ('Saving file: {0}' -f $newName)
Add-Content -Path $newName -Value $_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment