Reputation: 3135
With Powershell, I want to edit a file using :
Get-Content $inputFile | ForEach-Object {
$_.Replace("|Foo;","|Bar;")
} | Out-File $outputFile
This file is ASCII encoded but contains binary portions
Doing the same replacement with Notepad++ works !
Comparing the inputFile and the outputFile with Notepad++ I have some changes I didn't want :
The output file length (char count) has growed from the binary portion row count
Before the change Notepad++ detect the End Of Line as "UNIX", after as Dos\Windows (But the file use CRLF = Windows EOL)
In the binary portions, Notepad++ shows "CR" or "LF" EOL before transformation, and "CRLF" EOL
So it's clear that Powershell transforms some binary characters to windows End Of Line.
How to avoid it ?
Upvotes: 1
Views: 731
Reputation: 3135
Thanks to @wOxxOm who gave me the solution :
[IO.File]::WriteAllText(`
$outputFile, `
[IO.File]::ReadAllText($inputFile,[Text.Encoding]::GetEncoding("iso-8859-1")).Replace("|Foo;","|Bar;"),`
[Text.Encoding]::GetEncoding("iso-8859-1")
)
Edited taking account of @TomBlodget comment.
Upvotes: 1