-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplitCSV.ps1
42 lines (33 loc) · 981 Bytes
/
SplitCSV.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
param(
[parameter(Mandatory=$true,Position=0,ValueFromPipeLine=$true)]
$sourcefile,
[string[]]$selectfields,
$maxrow = 5000
)
$csv = Import-Csv $sourcefile
$pagenumber = 0
$indexinpage = 0
$pagedata = New-Object System.Collections.ArrayList
function WriteToCSV($pagedata, $pagenumber)
{
$pagedata | Export-Csv -Path $sourcefile.Replace(".csv", "_" + ($pagenumber.ToString(([int]($csv.Count / $maxrow).ToString() -replace "[0-9]", "0")) + ".csv")) -NoTypeInformation -Encoding ASCII
}
foreach ($row in $csv){
if ($selectfields.Count -eq 0)
{
[void]$pagedata.Add($row)
}
else {
$outdata = $row | select-object $selectfields
[void]$pagedata.Add($outdata)
}
$indexinpage++;
if ($indexinpage -ge $maxrow)
{
$indexinpage = 1;
$pagenumber++;
WriteToCSV -pagedata $pagedata -pagenumber $pagenumber
$pagedata.Clear();
}
}
WriteToCSV -pagedata $pagedata -pagenumber $pagenumber