batch-filecmdcommand-promptwindows-terminalcodepage-437

cmd.exe interprets ♪ as ? or ΓÖ¬ when reading .bat file on codepage 437


I wish to use the ♪ character inside a custom prompt for command prompt. This needs to be done from within my AutoRun batch file. chcp gives output Active code page: 437. If I type into the console via cmd.exe running on the Windows Terminal app prompt ♪$G, it works as expected, changing the prompt to ♪>.

However, if I type prompt ♪$G into a .bat file with vscode with encoding CP 437 or Windows 1252, the output is now:

prompt ?$G

?>

When saved with encoding UTF-8, the output is:

prompt ΓÖ¬$G

ΓÖ¬>

How can I change the prompt to ♪> from within a batch file? What encoding should the file be saved in? Will I need to use a hex editor in any way?


Solution

  • To use the ♪ character in a batch file, you need to save the file using the UTF-8 encoding.

    You can do this in most text editors, including VS Code.

    When you run the batch file, you may need to change the console code page to UTF-8 to ensure that the character is displayed correctly.

    You can do this by adding the following line to your batch file:


    chcp 65001
    

    This will change the console code page to UTF-8. After that, you can set the prompt using the ♪ character as follows:


    prompt ♪$G
    

    This should set the prompt to ♪>.

    You should not need to use a hex editor.


    For example you can test with batch file below :

    @echo off & cls
    Chcp 65001>nul
    prompt ♪$G
    CMD /K
    @echo on
    

    This batch file sets up the command prompt with a custom prompt character and then launches a new instance of the command prompt with the /K switch, which keeps the new command prompt window open after executing any commands.

    Here's a breakdown of the commands:

    1. @echo off turns off the display of each command in the command prompt, so they are not displayed as they are executed.
    2. cls clears the command prompt screen.
    3. Chcp 65001 sets the console code page to UTF-8, so that it can display special characters like the music note symbol (♪).
    4. prompt ♪$G sets the command prompt to display a music note symbol (♪) followed by a greater-than symbol (>) as the command prompt.
    5. CMD /K launches a new instance of the command prompt and keeps it open.
    6. @echo on turns on the display of each command in the new command prompt window.

    Edit :

    @echo off
    REM Get the current code page and save it in the ORIGINAL_CODEPAGE variable
    for /F "tokens=*" %%A in ('%SystemRoot%\System32\chcp.com') do for %%B in (%%A) do set /A "ORIGINAL_CODEPAGE=%%~nB" 2>nul
    
    REM Display the original code page to the user and wait for a key press
    echo My default current page is :%ORIGINAL_CODEPAGE%
    pause
    
    REM Set the console code page to UTF-8 (65001) and wait for a key press
    chcp 65001
    pause
    
    REM Set the command prompt to display a musical note (U+266A) followed by a greater-than symbol and wait for a key press
    prompt ♪$G
    pause
    
    REM Restore the original code page and launch a new command prompt that inherits the current environment variables
    chcp %ORIGINAL_CODEPAGE%
    CMD /K
    

    Changing the console code page to UTF-8 (65001) can cause issues when piping text between applications that use the C standard library, as some of those applications may not support UTF-8 encoding.

    This can result in characters being displayed incorrectly or not at all.

    However, in the batch file I provided in the edit section, the original console code page is saved at the beginning of the batch file, and then restored at the end of the batch file using the chcp command.

    This means that any negative side effects of changing the console code page to UTF-8 will be limited to the duration of the batch file, and the console code page will be restored to its original value before launching a new command prompt.

    So, as long as you are only using the UTF-8 console code page within the batch file itself, and not piping text to other applications that do not support UTF-8, you should not experience any negative side effects.