Alsatian
Alsatian

Reputation: 3135

Edit an ASCII encoded text file containing binary portions

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 :

So it's clear that Powershell transforms some binary characters to windows End Of Line.

How to avoid it ?

Before the transformation: Before the transformation

After the transformation: After the transformation

Upvotes: 1

Views: 731

Answers (1)

Alsatian
Alsatian

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

Related Questions