我正在运行以下Powershell脚本,将一系列输出文件合并成一个CSV文件。文件名为whidataXX.htm
(其中xx
是两位数字),创建的文件数量会因每次运行而异。
$metadataPath = "\\ServerPath\foo"
function concatenateMetadata {
$cFile = $metadataPath + "whiconcat.csv"
Clear-Content $cFile
$metadataFiles = gci $metadataPath
$iterations = $metadataFiles.Count
for ($i=0;$i -le $iterations-1;$i++) {
$iFile = "whidata"+$i+".htm"
$FileExists = (Test-Path $metadataPath$iFile -PathType Leaf)
if (!($FileExists))
{
break
}
elseif ($FileExists)
{
Write-Host "Adding " $metadataPath$iFile
Get-Content $metadataPath$iFile | Out-File $cFile -append
Write-Host "to" $cfile
}
}
}
whidataXX.htm
文件采用 UTF8 编码,但我的输出文件采用 UTF16 编码。当我在记事本中查看文件时,它显示正确,但当我在十六进制编辑器中查看时,每个字符之间都会出现十六进制值 00
,并且当我将文件导入 Java 程序进行处理时,控制台会在 c h a r a c t e r s
之间打印额外的空格。
首先,这是 PowerShell 的正常行为吗?还是源文件中有什么原因导致这种情况?
其次,我应该如何在上述代码中解决这个编码问题?