Function Out-FileUtf8NoBom {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=0)] [string] $LiteralPath,
[switch] $Append,
[switch] $NoClobber,
[AllowNull()] [int] $Width,
[Parameter(ValueFromPipeline)] $InputObject
)
[Environment]::CurrentDirectory = $PWD
$LiteralPath = [IO.Path]::GetFullPath($LiteralPath)
if ($NoClobber -and (Test-Path $LiteralPath)) {
Throw [IO.IOException] "The file '$LiteralPath' already exists."
}
$sw = New-Object IO.StreamWriter $LiteralPath, $Append
$htOutStringArgs = @{}
if ($Width) {
$htOutStringArgs += @{ Width = $Width }
}
try {
$Input | Out-String -Stream @htOutStringArgs | % { $sw.WriteLine($_) }
} finally {
$sw.Dispose()
}
}
Function FixEncoding([string] $path)
{
[IO.SearchOption] $option = [IO.SearchOption]::AllDirectories;
[String[]] $files = [IO.Directory]::GetFiles((Get-Item $path).FullName, '*.*', $option);
foreach($file in $files)
{
"Converting $file...";
Get-Content $file | Out-FileUtf8NoBom $file
}
}
你可以使用它作爲FixEncoding("C:\path\to\files\")
我相信這是在這裏找到答案。 http://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom –
你有超過ascii範圍的字符?如果不是,請使用'-enc ascii'。 –
@SunnyChakraborty正如OP指出的那樣,使用WriteAllLines並不是一個很好的選擇來輸出這個大的(〜3GB)。 –