# 只能轉與系統同編碼的ansi
function tounicode($orginFile = $(throw "input file required"))
{
$data = get-content $orginFile
set-content -value $data $orginFile -encoding utf8
}
# 可以自定來源編碼
function tounicode($orginFile = $(throw "input file required"), $coding = 'Shift-JIS')
{
#$data = get-content $orginFile
#set-content -value $data $orginFile -encoding utf8
#$orginFile = ($pwd).path + "\" + $orginFile;
$targetFile = $orginFile + "_tmp_";
$rr=New-Object IO.StreamReader($orginFile,[Text.Encoding]::GetEncoding($coding));
$ww=New-Object IO.StreamWriter($targetFile, $false, [Text.Encoding]::GetEncoding('UTF-8'));
while(!$rr.EndOfStream)
{$ww.WriteLine($rr.ReadLine());}
$rr.Close();$ww.Close();
del $orginFile;
ren $targetFile $orginFile;
}
dir -r | % {if ($_ -like "*.cpp" -or
$_ -like "*.c" -or
$_ -like "*.cc" -or
$_ -like "*.h" -or
$_ -like "*.hpp")
{
tounicode($_.fullname)
}
}
經測試自定來源編碼的轉法不會轉到已經是UTF-8編碼的檔案。