2017-09-06 65 views
0

我是PowerShell的新手,我有一個文本文件,我想使用Power Shell腳本知道它的編碼,我該怎麼做?使用PowerShell的文件格式

+0

見[此](https://stackoverflow.com/questions/5596982/using-powershell-to-write-a- file-in-utf-8-without-the-bom) –

+0

我讀過沒有幫助,謝謝你的幫助 –

+0

鏈接的問題及其答案是關於用特定編碼編寫文件,而不是關於檢測編碼一個現有的文件。 –

回答

0

這裏是一段代碼,我用google找到了這個代碼。下次嘗試谷歌之前,你問!

代碼例如:

$FilePath = "C:\temp\new.txt" 
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $FilePath 
if ($byte -ne $null) { 
    if ($byte.Count -ge 4) { 
     if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 
      Write-Output 'UTF8' 
     } elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { 
      Write-Output 'Unicode' 
     } elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { 
      Write-Output 'UTF32' 
     } elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { 
      Write-Output 'UTF7' 
     } else { 
      Write-Output 'ASCII' 
     } 
    } else { 
     Write-Error -Message ($Path + " is only " + $byte.Count + " bytes in size, unable to determine file encoding") -Category InvalidData 
    } 
} else { 
    Write-Error -Message ($Path + " is zero byte(s) in size") -Category InvalidData 
} 

實測值在網站上:http://sushihangover.blogspot.ch/2012/10/powershell-file-encoding-checking-and.html

+0

非常感謝您的幫助。 –

+0

@AmrHassan當它解決你的問題時,請將它標記爲答案。 – guiwhatsthat

+0

它不工作,我已經測試了幾次使用不同的格式,只顯示ASCII,你的答案沒有達到目的,反正謝謝 –