Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Get-Content -> Foreach-Object -> -replace ->Out-File is adding a char (0x00) to the start of every file

I have a function that performs a regex replace in a file. The problem is that that it adds a character (0x00) to the start of every file it touches (even the ones that it doesn't find a match for!). Since I am editing csproj files, MSBuild gives me this error:

error MSB4025: The project file could not be loaded. '.', hexadecimal value 0x00, is an invalid character. Line 2, position 1.

Here is my function:

function fileStringRegExReplace ([string] $fileToChange, [string] $oldString, [string] $newString) {
    echo "f" | xcopy "$fileToChange" "$fileToChange.og.cs" /Y /Q

    $file = Get-Content "$fileToChange.og.cs" | 
        Foreach-Object {
            $_ -replace $oldString, $newString
        } |
        Out-File "$fileToChange"

    Remove-Item "$fileToChange.og.cs"
}

How can I replace the lines I want and not change any other part of the file?

like image 291
Jeremiah Avatar asked Jan 14 '23 16:01

Jeremiah


2 Answers

It sounds like it's writing a BOM at the beginning of the file. You can set the encoding to ASCII (which has no BOM) using the -Encoding ASCII parameter on out-file.

like image 95
Nate Hekman Avatar answered Jan 31 '23 09:01

Nate Hekman


The default encoding of Out-File is Unicode, which is Windows-speak for UTF-16. When only writing characters from the ASCII set, UTF-16 basically has the effect of adding a 0x00 byte in front of each character. This explains why visual studio is complaining about 0x00 bytes.

The XML of the csproj files which you are trying to modify declare themselves to be UTF-8, so use the -Encoding UTF8 option in Out-File.

Do not use the ASCII encoding, this will cause problems as soon as the csproj file gets a non-ASCII character in it.

like image 43
Wim Coenen Avatar answered Jan 31 '23 09:01

Wim Coenen