Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active December 14, 2018 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/e2238e48faeddbb822fb2cccb45906ab to your computer and use it in GitHub Desktop.
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.
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
@gitfvb
Copy link
Author

gitfvb commented Dec 14, 2018

see encodings with [System.Text.Encoding]::GetEncodings()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment