views:

132

answers:

3

I am using Sandcastle Helpfile Builder to produce a helpfile (.chm). The project is a .shfbproj file, which is XML format, works with msbuild.

I want to automatically update the Footer text that appears in the generated .chm file. I use this snippet:

$newFooter = "<FooterText>MyProduct v1.2.3.4</FooterText>";

get-content  -Encoding ASCII $projFile.FullName | 
    %{$_ -replace '<FooterText>(.+)</FooterText>', $newFooter } > $TmpFile

move-item $TmpFile $projFile.FullName -force

The output directed to the $TmpFile is always a multi-byte string. But I don't want that. How do I set the encoding of the output to ASCII?

Anyone? Bueller? Anyone?

+3  A: 

You could change the $OutputEncoding variable before writing to the file. The other option is not to use the > operator, but instead pipe directly to Out-File and use the -Encoding parameter.

tomasr
tomas, gracias.
Cheeso
the > operator is actually an alias to out-file, which defaults to utf. tomasr is correct that you should use out-file specifically to set the encoding.
James Pogran
+2  A: 

The ">" redirection operator is a "shortcut" to out-file. Out file's default encoding is Unicode but you can chnage it to ASCII, so pipe to out-file instead:

get-content -Encoding ASCII $projFile.FullName | %{$_ -replace '(.+)', $newFooter } | out-file $tmpfile -encoding ASCII

Shay Levy
A: 

| sc filename does the trick (sc being an alias for Set-Content)

for >> filename use | ac filename does the trick (ac being an alias for Add-Content)

Ruben Bartelink