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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With