簡體   English   中英

echo Windows批次中的UTF-8個字符

[英]Echo UTF-8 characters in Windows batch

我可以使用 echo 生成 UTF-8 文本文件嗎? 例如,如果我想生成一個包含字符ę的文件:

echo "abcd ę" > out.txt

(批處理文件使用 UTF-8 編碼)

結果是一個 ANSI 編碼的文件,並且ę字符被轉換為ê 如何說服echo生成 UTF-8 文件?

如果不可能,那么我可以在創建文本文件后更改它的編碼嗎? gnuwin32 package 中是否有任何工具可以幫助我更改編碼?

謝謝

使用chcp命令將 utf-8 的活動代碼頁更改為 65001。

chcp 65001

嘗試使用 /U 開關啟動 CMD.exe:它會導致所有管道輸出為 Unicode 而不是 ANSI。

CHCP 65001

正如@cuixiping 所提到的,這是一個很好的答案,但它需要將 cmd 默認字體更改為Lucida 控制台,例如,您可以在這里閱讀: https : //superuser.com/questions/237081/whats-the-code-page-of -utf-8#272184

當然,正如@BearCode 所提到的,文本應該是 utf-8 ……在我的情況下,在 GNU/Linux 下使用 Vim 可以遠程訪問,但 notepad++ 也是正確的方式!

問題是該文件包含以下行:

<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type> 

然后 Notepad2 和 Firefox 正在更改字符集,顯示 Ä 而不是 ę。 在普通的記事本中,文件看起來不錯。 解決辦法是在文件開頭添加UTF-8簽名(字節順序標記):

echo1 -ne \xEF\xBB\xBF > out.htm

(echo1 來自 gnuwin32)

謝謝你的回答

出現以及更改代碼頁時,您需要在第一個回顯中寫入至少一個 unicode 字符到文件中,以便將文件保存為 unicode。 因此,您的批處理文件本身需要以 UTF-8 等 unicode 格式存儲。

I'm not sure if this is the answer you are looking for or if it's already been answered for you... 
I'd use the catet character ( ^ ) in a batch file and output to a file using escape character ^. See examples..
Desired output...
<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type> 

Replace code with this: 
Example 1: echo ^<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type^> 
Example 2: echo ^<?xml version="1.0" encoding="utf-8" ?^>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM