Text File Encoding ANSI to UTF-8

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Nel
Posts: 2
Joined: 09 Oct 2018 08:44

Text File Encoding ANSI to UTF-8

#1 Post by Nel » 09 Oct 2018 08:49

How to change the Text File Encoding?

ShadowThief
Expert
Posts: 908
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Text File Encoding ANSI to UTF-8

#2 Post by ShadowThief » 09 Oct 2018 12:18

The easiest way (short of opening the file with a text editor and saving it manually) is probably with ConvertCP.exe. Beyond that, my next thought would be to add the UTF-8 BOM to the front of the text file, but that might not be the best solution for some circumstances.

dbenham
Expert
Posts: 2283
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Text File Encoding ANSI to UTF-8

#3 Post by dbenham » 09 Oct 2018 14:56

I agree, aGerman's ConvertCP.exe utility would be an ideal solution. You want to translate to code page 65001. Add the /B option if you want a BOM in your output.

Alternatively, you could use my JREPL.BAT utility. It is designed to do regular expression find/replace operations on text files, but it has the ability to read and write different encodings.

The following assumes the "ANSI" source encoding matches the default encoding used by your machine:

Code: Select all

call jrepl "$^" "" /f "input.txt" /o "output.txt|utf-8"
The above will terminate every line with carriage return linefeed (\r\n), regardless how the source lines were terminated. If you want to preserve the original line terminators, then add the /M option.

By default the output will include the BOM. If you don't want a BOM, then use /O "output.txt|utf-8|nb".

If the ANSI encoding does not match your machines default, then you will have to specify the encoding within the /I option. For example, if your encoding is Central/Eastern European code page 1250, then

Code: Select all

call jrepl "$^" "" /f "input.txt|windows-1250" /o "output.txt|utf-8"

Nel
Posts: 2
Joined: 09 Oct 2018 08:44

Re: Text File Encoding ANSI to UTF-8

#4 Post by Nel » 10 Oct 2018 01:30

Thanks.

Post Reply