-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplit-File.psm1
201 lines (169 loc) · 7.27 KB
/
Split-File.psm1
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<#
.SYNOPSIS
PowerShell module to split one or many input files into smaller files with specified line count.
.DESCRIPTION
.EXAMPLE
PS C:\> Split-File foo.txt -Split 100 -Header 2 -AddHeaders
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
General notes
#>
function Get-Encoding {
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('FullName')]
[string]
$Path
)
process {
$bom = New-Object -TypeName System.Byte[](4)
$file = New-Object System.IO.FileStream($Path, 'Open', 'Read')
$null = $file.Read($bom,0,4)
$file.Close()
$file.Dispose()
$enc = "Default"
if ($bom[0] -eq 0x2b -and $bom[1] -eq 0x2f -and $bom[2] -eq 0x76)
{ $enc = "UTF7" }
if ($bom[0] -eq 0xff -and $bom[1] -eq 0xfe)
{ $enc = "Unicode" }
if ($bom[0] -eq 0xfe -and $bom[1] -eq 0xff)
{ $enc = "BigEndianUnicode" }
if ($bom[0] -eq 0x00 -and $bom[1] -eq 0x00 -and $bom[2] -eq 0xfe -and $bom[3] -eq 0xff)
{ $enc = "UTF32" }
if ($bom[0] -eq 0xef -and $bom[1] -eq 0xbb -and $bom[2] -eq 0xbf)
{ $enc = "UTF8" }
$enc
}
}
function Split-File {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName = $true,
HelpMessage="Path to one or more files.")]
[ValidateNotNullOrEmpty()]
[SupportsWildcards()]
[string[]] $Path,
[Parameter(Mandatory=$false,
Position=1,
HelpMessage="Export path of converted file")]
[string[]] $ExportPath,
[Parameter(Mandatory=$true,
HelpMessage="Number of lines to split input file on")]
[ValidateRange(1, [int]::MaxValue)]
[int] $Split,
[Parameter(Mandatory=$false,
HelpMessage="Number of header rows in input file (defaults to 1)")]
[ValidateRange(0, [int]::MaxValue)]
[int] $Header = 1,
[Parameter(Mandatory=$false,
HelpMessage="Skip header from original file in resulting files (defaults to false")]
[switch] $SkipHeader,
[Parameter(Mandatory=$false,
HelpMessage="Encoding to use on both input and output files,tries to guess encoding if not specified")]
[ValidateSet('Default','ASCII','UTF7','UTF8', 'Unicode','UTF32', 'BigEndianUnicode')]
[string] $Encoding,
[Parameter(Mandatory=$false,
HelpMessage="Include batch/chunk in the filename instead of current number in the sequence (defaults to falsee)")]
[switch] $BatchNaming
)
begin {
if (Test-Path -PathType Leaf -Path $Path) {
$files = Get-ChildItem -File $Path
} else {
Write-Error "Invalid path"
break
}
# Set up the export path
if (!$ExportPath -or !(Test-Path $ExportPath -PathType Container)) {
$ExportPath = Split-Path $Path -Resolve
}
}
process {
$Processed = 0
foreach ($file in $files) {
$FilePath = ((Resolve-Path $file.FullName).ToString() -replace "(^.*::)")
$ProgressActivity = "Splitting $($file.Name):"
Write-Host "Splitting $($file.Name)"
Write-Progress -Activity $ProgressActivity `
-Status "Calculating total number of output files" `
-PercentComplete 0
if (-not $Encoding) {
$Encoding = Get-Encoding -Path ((Resolve-Path $Path).ToString() -replace "(^.*::)")
}
$EncodingObject = switch ($Encoding) {
"UTF7" { [System.Text.Encoding]::UTF7 ; break }
"Unicode" { [System.Text.Encoding]::Unicode ; break }
"BigEndianUnicode" { [System.Text.Encoding]::BigEndianUnicode ; break }
"UTF32" { [System.Text.Encoding]::UTF32 ; break }
"UTF8" { [System.Text.Encoding]::UTF8 ; break }
default { [System.Text.Encoding]::Default ; break }
}
try {
$FileReader = New-Object System.IO.StreamReader $FilePath, $EncodingObject
} catch {
Write-Error "Error: Could not open $($FilePath)"
break
}
$TotalLines = -$Header + $((Get-Content $file -ReadCount 1000 -Encoding "Default" | ForEach-Object {$x += $_.Count });$x)
$TotalBatches = [Math]::Ceiling($TotalLines / $Split)
$FileHeader = [System.Collections.ArrayList]@()
$Counter = 0
while ($Counter -lt $Header) {
if (($Counter % ($Header / 20)) -eq 0 -or $Counter -eq 0) {
Write-Progress -Activity $ProgressActivity `
-Status "Reading header" `
-PercentComplete ([Math]::Ceiling($Counter / $Header * 100))
}
$null = $FileHeader.Add($FileReader.ReadLine())
$Counter++
}
$Batch = 1
$Counter = 1
while ($FileReader.EndOfStream -ne $true) {
$CurrentBatch = if ($Batch -eq $TotalBatches) { $TotalLines } else { $Counter + $Split - 1 }
$Suffix = if ($BatchNaming) { $Counter.ToString() + "-" + ($CurrentBatch).ToString() }
else { $Batch.ToString().PadLeft($TotalBatches.ToString().Length, '0') }
$ExportFile = (Join-Path -Path $ExportPath `
-ChildPath ($file.BaseName + "_" `
+ $Suffix `
+ $file.Extension)).ToString() -replace "(^.*::)"
Write-Host "-> $ExportFile"
Write-Progress -Activity $ProgressActivity `
-Status "Writing $ExportFile (of $TotalBatches)" `
-PercentComplete ([Math]::Ceiling($Batch / $TotalBatches * 100))
try {
$FileWriter = New-Object System.IO.StreamWriter $ExportFile, $false, $EncodingObject
} catch {
$_.Exception.Message
Write-Error "Error: Could not write file: $ExportFile"
break
}
if (!$SkipHeader) {
foreach ($line in $FileHeader) {
$FileWriter.WriteLine($line)
}
}
while ($Counter -le $CurrentBatch -and $FileReader.EndOfStream -ne $true) {
$FileWriter.WriteLine($FileReader.ReadLine())
$Counter++
}
$FileWriter.Close()
$Batch++
}
$FileReader.Close()
$Processed++
}
}
end {
$FileReader.Close() | Out-Null
$FileWriter.Close() | Out-Null
Write-Host "`n$Processed file(s) processed`n"
}
}
Export-ModuleMember -Function Split-File