Last active
December 14, 2018 16:43
-
-
Save gitfvb/e2238e48faeddbb822fb2cccb45906ab to your computer and use it in GitHub Desktop.
Rewrite a text file via powershell and skip some lines and/or change encoding. This way is very efficient and suitable for large files.
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
function rewriteFile() { | |
param( | |
[Parameter(Mandatory=$true)][string]$inputPath, | |
[Parameter(Mandatory=$true)][int]$inputEncoding, | |
[Parameter(Mandatory=$true)][string]$outputPath, | |
[Parameter(Mandatory=$true)][int]$outputEncoding, | |
[Parameter(Mandatory=$false)][int]$skipFirstLines | |
) | |
$input = Get-Item -Path $inputPath | |
$reader = New-Object System.IO.StreamReader($input.FullName, [System.Text.Encoding]::GetEncoding($inputEncoding)) | |
$tmpFile = "$( $input.FullName ).$( [datetime]::Now.ToString("yyyyMMddHHmmss") ).tmp" | |
$append = $false # the true means to "append", false means replace | |
$writer = New-Object System.IO.StreamWriter($tmpFile, $append, [System.Text.Encoding]::GetEncoding($outputEncoding)) | |
for ($i = 0; $i -lt $skipFirstLines; $i++) { | |
$reader.ReadLine() > $null # Skip first line. | |
} | |
while ($reader.Peek() -ge 0) { | |
$writer.writeline($reader.ReadLine()) | |
} | |
$reader.Close() | |
$writer.Close() | |
Remove-Item -Path $outputPath -Recurse | |
Move-Item -Path $tmpFile -Destination $outputPath | |
} | |
#example for skipping the first line | |
rewriteFile -inputPath "20181214164147_success.txt" -outputPath "20181214164147_success_2.txt" -inputEncoding ([System.Text.Encoding]::UTF8.CodePage) -outputEncoding ([System.Text.Encoding]::UTF8.CodePage) -skipFirstLines 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see encodings with [System.Text.Encoding]::GetEncodings()