在PowerShell Out-File cmdlet没有Utf8NoBOM
编码。
但是,您可以使用.NET编写不带BOM的Utf8文本文件:
以下所有方法的共同点
$rootFolder = 'D:\test' # the path where the textfiles to merge can be found
$outFile = Join-Path -Path $rootFolder -ChildPath 'newfile.txt'
方法1
# create a Utf8NoBOM encoding object
$utf8NoBom = New-Object System.Text.UTF8Encoding $false # $false means NoBOM
Get-Content -Path "$rootFolder\*.txt" -Encoding UTF8 -Raw | ForEach-Object {
[System.IO.File]::AppendAllText($outFile,$_,$utf8NoBom)
}
方法2
# create a Utf8NoBOM encoding object
$utf8NoBom = New-Object System.Text.UTF8Encoding $false # $false means NoBOM
Get-ChildItem -Path $rootFolder -Filter '*.txt' -File | ForEach-Object {
[System.IO.File]::AppendAllLines($outFile,[string[]]($_ | Get-Content -Encoding UTF8),$utf8NoBom)
}
方法3
# Create a StreamWriter object which by default writes Utf8 without a BOM.
$sw = New-Object System.IO.StreamWriter $outFile,$true # $true is for Append
Get-ChildItem -Path $rootFolder -Filter '*.txt' -File | ForEach-Object {
Get-Content -Path $_.FullName -Encoding UTF8 | ForEach-Object {
$sw.WriteLine($_)
}
}
$sw.Dispose()
,
没有-encoding参数,PS 5(gc)无法处理utf8没有bom输入文件:
(gc -Encoding Utf8 *.txt) | out-file newfile.txt -encoding utf8
本文链接:https://www.f2er.com/3139003.html