Last active
April 7, 2021 20:55
-
-
Save peetrike/0e96462b5e227057a33a4aa1ec31d213 to your computer and use it in GitHub Desktop.
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
<# | |
.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