CHCP - CMD
Overview
The CHCP
command in Windows Command Prompt is used to display or set the active console code page. Code pages are sets of characters that define how characters are represented in a text file. This command is particularly useful in scenarios involving multilingual text data where diverse character encodings are necessary for proper display and input in the console window.
Syntax
The CHCP
command follows this basic syntax:
CHCP [nnn]
nnn
is the code page number that specifies the new active code page. Ifnnn
is not provided,CHCP
displays the currently active code page.
Options/Flags
CHCP does not have additional flags or options. You only specify the code page number if you wish to change the current setting. Leaving the argument empty will default the function to displaying the current code page.
Examples
-
Displaying the Current Code Page:
C:\> CHCP Active code page: 437
This command displays the current code page used by the console.
-
Changing the Code Page to 65001 (UTF-8):
C:\> CHCP 65001 Active code page: 65001
This is useful when you need to handle UTF-8 encoded text files in the command prompt.
-
Switching between multiple code pages in a script:
If a batch file processes files from different locales, you might need to switch code pages:
CHCP 850 > nul REM Process a file encoded in Western European character set CHCP 437 > nul REM Switch back to the default code page
This example shows how to temporarily change the code page to handle specific files, then revert back.
Common Issues
- Error “Invalid code page”: Ensure the code page number is valid and available on your system.
- Command does not affect the Windows GUI:
CHCP
changes only affect console applications. GUI applications do not respect this change.
Integration
The CHCP
command can be integrated with other CMD commands to handle encoding issues in file operations or scripting tasks. For example, combining with FOR
loop to process multiple files:
FOR %f IN (*.txt) DO (
CHCP 65001
TYPE %f
CHCP 437
)
This script processes text files that are in UTF-8 encoding and then reverts to the default code page.
Related Commands
- CMD: The command interpreter itself, where
CHCP
is executed. - Type: Displays the content of a text file. Using
CHCP
can change how content is interpreted or displayed.
For more detailed information on code pages and how they are used in Windows, you can refer to the Windows documentation on character sets.