Programming Field

Chcp - DOS/Command Prompt Reference

(acronym for Change Code page)

Changes the current (active) code page.

Syntax

chcp[.com] [nnn]
nnn
Specifies the code page number. For example 437 for English and 932 for Japanese is used. For available code pages, please see Available code pages.
If omitted, Chcp displays the current (active) code page.

Details

Usage of Chcp

Changing the code page (character code) affects character types not only that can be displayed on the prompt, but also that is used for file I/O.

If the code page is changed in a batch file, the changed code page remains on the prompt after the batch file exits. If you change the code page temporarily, you need to restore it explicitly. ([Windows NT series] Changing the code page is not affected by localization of Setlocal; Endlocal will not reset the code page.)

[Windows NT series?/XP or later] The current code page can also be changed by executing mode CON CODEPAGE SELECT=xxx. The difference between Mode command and Chcp command is that Mode does not clear the screen (Chcp does).

Available code pages

[MS-DOS] Available code pages are determined by COUNTRY statement in Config.sys. To use multi language, it is necessary to load appropriate COUNTRY data.

[Windows XP? or later] Mainly the following code pages can be used (code pages not listed below may also be available).

NumberCharacter code
437English (OEM America)
932Japanese (Shift JIS)
20932EUC-JP
50220JIS
65000UTF-7
65001UTF-8

* 1200 (UTF-16LE) is not available.

Code pages and actual outputs

Command Prompt uses the code page, specified by Chcp, for file input and pipe input, but if Command Prompt is executed with ‘/u’ option (see cmd.exe page), the output will be Unicode(UTF-16). Specifically, the patterns are as follows.

Chcpcmd /u switchInput formatOutput format
932NoShift JISShift JIS
20932NoEUC-JPEUC-JP
65000NoUTF-7UTF-7
65001NoUTF-8UTF-8
932YesShift JISUTF-16
20932YesEUC-JPUTF-16
65000YesUTF-7UTF-16
65001YesUTF-8UTF-16

When UTF-16 BOM exists, Type command and More command reads its file as UTF-16 (not sure about other commands).

[Windows 10] When the current code page is changed, (if the font of current Command Prompt window supports) characters for the code page may be printed correctly. Under UTF-8 code page Japanese can be displayed for example (some characters may be garbled), so writing ‘chcp 65001’ in the head of the batch file allows to write the batch file in UTF-8, including non-ASCII characters. However, it seems that 4-byte UTF-8 character cannot be displayed in Windows 10.

[Windows 11] Windows Terminal can display 4-byte UTF-8 character correctly (Windows Console Host cannot).

Samples

Sample (Batch file)

@echo off
setlocal
setlocal enabledelayedexpansion
set TEMPLATE_FILE=D:\Data\Template\hoge.xml
set OUT_FILE=D:\MyData\bar.xml
set MY_VERSION=123

for /F "tokens=2 delims=:" %%P in ('chcp') do (
    set ACTIVE_CP=%%P
)

chcp 65001 > NUL

type NUL > "%OUT_FILE%"
for /F "usebackq delims=" %%t in ("%TEMPLATE_FILE%") do (
    set TEMP_LINE=%%t
    set TEMP_LINE=!TEMP_LINE:[version]=%MY_VERSION%!
    echo !TEMP_LINE!>> "%OUT_FILE%"
)

chcp %ACTIVE_CP% > NUL

[Windows NT series] [Extensions] This is UTF-8 support version of creating the new file from a template file with replacing with some characters, described in For command sample. By changing the code page to ‘65001’ (UTF-8), UTF-8 will be used for I/O, so the file with UTF-8 encoding can be used safely.

The first For command and Chcp perform to store the current code page to the environment variable ‘ACTIVE_CP’. At the end, the code page will be restored by calling Chcp again.

* The reason why parsing by this For command is valid is Chcp outputs the current code page like the format ‘Active code page: 437’.
* ‘ACTIVE_CP’ may include the space character before the numeric value, but even in this case expanding ‘chcp %ACTIVE_CP%’ will be two (or more) space characters between ‘chcp’ and the numeric, resulting no problem.

See also