1

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?

Amber Cahill
  • 106
  • 8
  • 1
    these characters don't exist in CP437. You need to use `chcp 65001` to switch to UTF-8 at the beginning and save the script as UTF-8 – phuclv Feb 14 '23 at 06:11
  • 1
    The Wikipedia article about [code page](https://en.wikipedia.org/wiki/Code_page) has a list of all code pages and a link to [OEM](https://en.wikipedia.org/wiki/Original_equipment_manufacturer) [code page 437](https://en.wikipedia.org/wiki/Code_page_437) and [Windows-1252](https://en.wikipedia.org/wiki/Code_page_1252). Both code pages do no contain the character `♪` which is the reason why Visual Studio Code saved the batch file [UTF-8](https://en.wikipedia.org/wiki/UTF-8) encoded. – Mofi Feb 14 '23 at 07:09
  • 2
    @Mofi The code page 437 wikipedia article you linked literally does contain the `♪` character at position `0x0D` – Amber Cahill Feb 14 '23 at 08:52
  • When you write/save your batch file @AmberCahill, do so with an editor which can save the script using the target codepage, e.g. DOS/OEM 437. When the file is saved using the same codepage stipulated within that script, all of its characters should be faithfully reproduced. – Compo Feb 14 '23 at 10:41
  • 1
    @AmberCahill The characters with code point value 0x00 to 0x1F are control characters according to [ASCII](https://www.asciitable.com/). Please read the paragraph above the table. The character with value 0x0D is a carriage return (CR) which is a newline character existing at end of every line in a batch file together with the new line character line feed (0x0A, LF). `cmd.exe` does not interpret a byte with hexadecimal value 0x0D as eighth note but as carriage return. – Mofi Feb 14 '23 at 14:23

1 Answers1

3

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.

Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Will this approach keep the codepage at 65001? I've heard there are negative side effects of only using that codepage, especially when piping between applications that use the C standard library. – Amber Cahill Feb 14 '23 at 08:58
  • @AmberCahill Check my last edit ! – Hackoo Feb 14 '23 at 10:28
  • Hackoo is absolutely right with the information that `♪` could be not displayed at all. The prompt is displayed just with `>` on Windows 7 because of the default font for a Windows console window is `Raster Fonts` on Windows 7/Vista/XP which means the OEM font `Terminal` which is a bitmap font (font file `%SystemRoot%\Fonts\vga850.fon` in my case on Windows 7). There is no bitmap available for the eighth note. The usage of a code page respectively character encoding supporting this character does not mean that the configured console font supports that character too. – Mofi Feb 14 '23 at 14:47
  • There is available on Windows 7 also `Consolas` which is the default console window font since Windows 8. The batch file posted by Hackoo displays the prompt with `♪>` if I open the properties of the console window and change the font from `Raster Fonts` to `Consolas`. There is unfortunately no [Windows command](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) to change the console font for the active console window. It is better for maximum console window compatibility to avoid the usage of UTF-8 encoded characters like `♪`. – Mofi Feb 14 '23 at 14:58
  • Batch files can be executed by a user also from within a PowerShell console window or a Windows Terminal window on which the font and the default code page (character encoding) is also different to a Windows command prompt window. I avoid UTF-8 encoded batch files for that reason as much as possible on being executed in an unknown environment as executed by other people on their Windows computers. – Mofi Feb 14 '23 at 14:58