-
Notifications
You must be signed in to change notification settings - Fork 30
/
SplitCsv.ps1
66 lines (51 loc) · 1.82 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#############
# Powershell script to split very large csv files
# Example command that split input csv into smaller csv's of 500000 lines in each, and preserving csv header:
# SplitCsv.ps1 C:\temp\Mft_2015-10-04_00-41-41.csv -d C:\temp\Output -l 500000
#############
[CmdletBinding(DefaultParameterSetName='Path')]
param(
[Parameter(ParameterSetName='Path', Position=1, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$Path,
[Alias('d')]
[Parameter(Position=2)]
[String]$Destination='.',
[Alias('l')]
[Parameter(Position=3,Mandatory=$true)]
[Int32]$TargetLines
)
$InputFile = $Path
If(!(Test-Path $Destination)){Throw "Error: Destination path does not exist";}
If(!(Test-Path $InputFile)){Throw "Error: Input file does not exist";}
$sw = new-object System.Diagnostics.Stopwatch
$sw.Start()
Write-Host "Reading source file..."
$lines = [System.IO.File]::ReadAllLines($InputFile)
$totalLines = $lines.Length
Write-Host "Total Lines :" $totalLines
$skip = 0
$count = $TargetLines; # Number of lines per file
# File counter, with sort friendly name
$fileNumber = 1
$fileNumberString = $filenumber.ToString("000")
while ($skip -le $totalLines) {
$upper = $skip + $count - 1
if ($upper -gt ($lines.Length - 1)) {
$upper = $lines.Length - 1
}
$FileChunk = Join-Path $Destination "result$fileNumberString.csv"
# Write the lines
if($skip){
Set-Content $FileChunk –value $lines[0]
Add-Content $FileChunk –value $lines[$skip..$upper]
}
Else{
[System.IO.File]::WriteAllLines($FileChunk, $lines[($skip..$upper)])
}
# Increment counters
$skip += $count
$fileNumber++
$fileNumberString = $filenumber.ToString("000")
}
$sw.Stop()
Write-Host "Split complete in " $sw.Elapsed.TotalSeconds "seconds"