我是PowerShell的新手,我有一個文本文件,我想使用Power Shell腳本知道它的編碼,我該怎麼做?使用PowerShell的文件格式
0
A
回答
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,你的答案沒有達到目的,反正謝謝 –
相關問題
- 1. 格式文本文件使用PowerShell
- 2. Powershell格式文本文件
- 3. 重新格式化文件,以管格式使用PowerShell
- 4. PowerShell輸出文件格式
- 5. 使用PowerShell在文本文件中格式化輸出
- 6. 帶有Powershell的UNIX格式文件
- 7. 在PowerShell中使用Awk的文件格式
- 8. Powershell&nice格式化輸出文件
- 9. Powershell:格式化可執行文件
- 10. Powershell重新格式化文件內容
- 11. 如何在PowerShell中格式化文件
- 12. 上傳文件格式powershell到asp.net
- 13. 從文件創建數組並使用Powershell進行格式化
- 14. 使用PowerShell和「文件格式」錯誤編輯XML
- 15. PowerShell使用format.ps1.xml文件格式化自定義對象
- 16. 使用Powershell創建ACH文件給出格式錯誤
- 17. C++使用文件格式
- 18. Powershell格式
- 19. Powershell格式
- 20. 日曆使用powershell格式化
- 21. 如何添加格式使用PowerShell
- 22. 格式表中未使用PowerShell 5
- 23. 發送PowerShell電子郵件時的格式txt文件
- 24. 使用PowerShell連接文件
- 25. 使用PowerShell打開文件
- 26. 使用PowerShell以CSV格式轉換日期格式
- 27. PowerShell的 - 格式化日期YYMMDD格式
- 28. 使用PowerShell加載文件文件?
- 29. 使用Spark格式化文本文件
- 30. 使用jquery格式化文本文件
見[此](https://stackoverflow.com/questions/5596982/using-powershell-to-write-a- file-in-utf-8-without-the-bom) –
我讀過沒有幫助,謝謝你的幫助 –
鏈接的問題及其答案是關於用特定編碼編寫文件,而不是關於檢測編碼一個現有的文件。 –