Enabling Special Characters in Windows 10

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Enabling Special Characters in Windows 10

#16 Post by aGerman » 17 Sep 2021 11:06

It would have been much easier to directly write the ╦ into your script code and save the script in the default OEM encoding of your system. A more portable way is to save it UTF-8-encoded (without BOM) and change the console codepage to UTF-8 at the beginning of your script using

Code: Select all

>nul chcp 65001
Don't rely on byte 0xcb representing the "Box Drawings Double Down and Horizontal". This is only true for certain OEM code pages. Say, I'm Japanese, and my default OEM code page is 932, then byte 0xcb will be rendered as ヒ for me.

Steffen

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Enabling Special Characters in Windows 10

#17 Post by atfon » 17 Sep 2021 11:59

aGerman wrote:
17 Sep 2021 11:06
Don't rely on byte 0xcb representing the "Box Drawings Double Down and Horizontal". This is only true for certain OEM code pages. Say, I'm Japanese, and my default OEM code page is 932, then byte 0xcb will be rendered as ヒ for me.

Steffen
Oh, I agree and should have included that caveat. I was just indicating another way one could obtain the extended character sets, even with the code page I use. I have used the method you are suggesting several times. I was just pleased to find alternative methods. I suppose I could also check the Code Page, set it as a variable, change to the one I want and set it back when the script ends, replacing 437 with whatever code page you want.
for /f "delims=: tokens=2" %%g in ('chcp') do set /a "cPage=%%~ng"
>nul echo %cPage% | findstr "437"
if %errorlevel% NEQ 0 chcp 437
:: Do stuff
>nul chcp %cPage%
endlocal
exit
Edit: Add a method to potentially temporarily reset the code page. Also, I fixed a mistake in originally capturing the code page number.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Enabling Special Characters in Windows 10

#18 Post by aGerman » 17 Sep 2021 14:22

atfon wrote:
17 Sep 2021 11:59
>nul echo %cPage% | findstr "437"
if %errorlevel% NEQ 0 chcp 437
Why so complicated? You could just compare the two numbers. But that's still unnecessarily complicated. Simply always execute CHCP 437 and you're done.
atfon wrote:
17 Sep 2021 11:59
endlocal
exit
Nit: Both are not needed at all. ENDLOCAL is implicitely executed if you leave the script (for every SETLOCAL in the script). Also, the script execution ends at the end of the script. EXIT is not even what you want to do in many cases. It always terminates cmd.exe.

Steffen

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Enabling Special Characters in Windows 10

#19 Post by atfon » 17 Sep 2021 14:45

aGerman wrote:
17 Sep 2021 14:22

Why so complicated?

Nit: Both are not needed at all.

Steffen
You're right. I was overthinking it. Nit away! That's how we learn! :D Thanks, Steffen

Post Reply