Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save text file in UTF-8 encoding using cmd.exe

Is it possible to save a text file in UTF-8 encoding using Windows' command line cmd.exe?

Current I'm creating the text file using command redirection operators:

C:\Windows\system32\ipconfig /all >> output.log

I start with > to get a new file and then I use >> to append more information.

like image 867
user2333346 Avatar asked May 16 '13 22:05

user2333346


People also ask

How do I change the Encoding to UTF-8?

Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.


2 Answers

The default encoding for command prompt is Windows-1252. Change the code page (chcp command) to 65001 (UTF-8) first and then run your command.

chcp 65001
C:\Windows\system32\ipconfig /all >> output.log

Change it back to default when done.

chcp 1252
like image 104
rchn Avatar answered Oct 23 '22 01:10

rchn


As the existing answer says, in a batch file, you can use the chcp command

chcp 65001 > nul
some_command > file

But if you are using the cmd.exe from its command line, e.g. to execute a user-defined command, you can use this syntax:

cmd.exe /c chcp 65001 > nul & cmd.exe /c some_command > file
like image 22
Martin Prikryl Avatar answered Oct 23 '22 02:10

Martin Prikryl