Echo UTF-8 characters in windows batch

后端 未结 5 1790
暖寄归人
暖寄归人 2021-02-03 22:41

Can I use echo to generate an UTF-8 text file? For example if I want to generate a file that contains the character \"ę\"

echo \"abcd ę\" > out.txt

相关标签:
5条回答
  • 2021-02-03 22:46

    The problem was that the file contained the line:

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

    and then Notepad2 and Firefox was changing the charset, showing Ä instead of ę. In plain Notepad, the file looks ok. The solution was to add the UTF-8 signature (Byte Order Mark) at the beginning of the file:

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

    (echo1 is from gnuwin32)

    thanks for the answers

    0 讨论(0)
  • 2021-02-03 22:47

    Try starting CMD.exe with the /U switch: it causes all pipe output to be Unicode instead of ANSI.

    0 讨论(0)
  • 2021-02-03 23:03

    Use chcp command to change active code page to 65001 for utf-8.

    chcp 65001
    
    0 讨论(0)
  • 2021-02-03 23:04

    chcp 65001

    as mention by @cuixiping is a good answer but it require to change cmd default font to Lucida Console for example, as you can read here: https://superuser.com/questions/237081/whats-the-code-page-of-utf-8#272184

    and of course, as mentioned by @BearCode, the text should be in utf-8… in my case, with Vim under GNU/Linux with remote access, but notepad++ is right way too!

    0 讨论(0)
  • 2021-02-03 23:04

    Appears as well as changing the code page you need to write at least one unicode character in your first echo out to the file for the file to be saved as unicode. So your batch file itself needs to be stored in a unicode format like UTF-8.

    0 讨论(0)
提交回复
热议问题