Page 1 of 1

How to create an UTF-8 file (instead of UCS-2 Little Endian)?

Posted: 28 Nov 2018 05:10
by pstein
Wehn I issue from a DOS batch file the following command:

echo foobar >myfile.txt

then myfile.txt will always be created as UCS-2 Little Endian based file

How can I tell the DOS batch execution to create an UTF-8 file instead?

Peter

Re: How to create an UTF-8 file (instead of UCS-2 Little Endian)?

Posted: 28 Nov 2018 11:34
by aGerman
pstein wrote:
28 Nov 2018 05:10
then myfile.txt will always be created as UCS-2 Little Endian based file
Serioulsly? I can't believe that. It's definitely not the default behavior.
pstein wrote:
28 Nov 2018 05:10
How can I tell the DOS batch execution to create an UTF-8 file instead?
The Windows has kind of UTF-8 support. It's rather bad though.

Code: Select all

@echo off
>nul chcp 1252
set "x=åéøü"
>nul chcp 65001
>"utf8test.txt" echo %x%
... where 1252 is the code page that I used to save the script. 65001 is the code page for UTF-8. The output is the UTF-8-encoded representation of the string. Although the file doesn't contain the UTF-8 byte order mark at the beginning.

Steffen

Re: How to create an UTF-8 file (instead of UCS-2 Little Endian)?

Posted: 28 Nov 2018 13:38
by Squashman
I thought we had a thread going on the forum about creating a UTF-8 file with a BOM. I can't find it.

Re: How to create an UTF-8 file (instead of UCS-2 Little Endian)?

Posted: 28 Nov 2018 15:32
by aGerman
I'm absolutely sure we have several threads with examples of how to write the BOM. Everyone has the possibility to search for UTF-8 in the forum.
Nevermind.

Code: Select all

@echo off
>nul chcp 1252
set "x=åéøü"
<nul >"utf8test.txt" set /p "="

>nul chcp 65001
>>"utf8test.txt" echo %x%
Still dependent on the encoding used in the editor to save the script. Other possibilities would have been to use UTF-7 or CERTUTIL. Also it's not a big deal to use JREPL.BAT or CONVERTCP.exe to convert the file afterwards to any encoding you want. Both tools to be found in this forum, too.

Steffen