-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk-csv.ps1
More file actions
37 lines (29 loc) · 1.04 KB
/
chunk-csv.ps1
File metadata and controls
37 lines (29 loc) · 1.04 KB
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
<#
takes a csv and partitions it into chunks
of the given @size
#>
function chunk-file($file, [int]$size = 500) {
$lines = get-content $file
$i = 0;
$low = 1;
$count = $lines.length;
$high = [math]::min($count, $size - 1);
$leftPad = [math]::Log10($count / $size) + 1;
$start = $file.lastIndexOf("\") + 1;
$end = $file.lastIndexOf(".")
$name = $file.substring($start, $end - $start);
$header = @($lines[0])
do {
$i++;
# ensures that we include the header.
$slice = $header + $lines[$low..$high]
$ordinal = "_" + $i.ToString().PadLeft($leftPad, "0");
$chunkName = "chunks/$name" + $ordinal + ".csv"
$slice | out-file $chunkName -Encoding ascii
write-host "file written: $chunkName" -ForegroundColor Green
if ($high -eq $count - 1) { break; }
$low = $high + 1;
$high = [math]::min($count - 1, $low + $size);
} while ($true);
write-host "$i file(s) exported!" -ForegroundColor Green
}