Friday, June 15, 2007

A little correction to Process-File function

Once after processing my C++ source code tree with Process-File my sources became uncompilable. The problem was caused by redirection operator `>', which transformed source files from plain-old ASCII to Unicode. So there's corrected version which uses Out-File cmdlet instead with encoding specified explicitly:


function Process-File([scriptblock]$script,
$filename,
$encoding = "ascii") {
$local:temp = New-TempFileName
Get-Content -Encoding $encoding $filename | `
Foreach $script | `
out-file -encoding $encoding $local:temp
Move-Item -force $local:temp $filename
}

No comments: