Box-drawing characters in batch files...?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Box-drawing characters in batch files...?

#16 Post by Aacini » 09 Feb 2017 13:16

esuhl wrote:So... a computer won't necessarily support all valid code pages...? Is the only reliable solution to stick to pure ASCII characters?

You may use Pixel Resolution Graphics and then draw in the screen any desired "character", as shown in this example:

Image

Just an idea... :wink:

Antonio

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

Re: Box-drawing characters in batch files...?

#17 Post by aGerman » 09 Feb 2017 14:09

Hmm. I'm afraid you didn't fully understand.

esuhl wrote:and use the equivalent characters (accented E, etc.)

This isn't portable. See the difference between Antonio's and Saso's examples. Both are able to write box-drawing characters for their specific computer settings. As I wrote, as soon as you have characters in the code that exceed the ASCII range it won't be portable anymore.


esuhl wrote:So... a computer won't necessarily support all valid code pages...?

Right. And fonts won't support all possible characters (I'm aware of only one project where developers try to achieve this).

It's important to understand the difference between files-charset vs. window-font.
A file consists of bits and bytes. You need a translation between the bytes read (or written) and the characters that shall be represented by these bytes. The charset (code page) is just the rule for this translation.
A window needs a font for graphically displaying these characters. Even if the translation of the bytes read succeed the characters can't be displayed if the font don't support them (that's what happened to Saso).

Steffen

esuhl
Posts: 9
Joined: 04 Feb 2017 22:03

Re: Box-drawing characters in batch files...?

#18 Post by esuhl » 10 Feb 2017 07:27

aGerman wrote:Hmm. I'm afraid you didn't fully understand.

esuhl wrote:and use the equivalent characters (accented E, etc.)

This isn't portable. See the difference between Antonio's and Saso's examples. Both are able to write box-drawing characters for their specific computer settings. As I wrote, as soon as you have characters in the code that exceed the ASCII range it won't be portable anymore.


Ah. Thanks for correcting me. I've re-read this thread several times, but I still don't quite understand why this wouldn't work.

Going back to your example, let's say I want to echo the õ character to screen. Notepad on your system uses a different encoding, so you enter the character as ä, but doing chcp 850 will (I thought) make sure the character appears correctly on all systems...?

So long as the batch file contains byte E4, and the console is set to chcp 850... I don't understand why this wouldn't work...? (Other than the console font not having such a character.)

aGerman wrote:
esuhl wrote:So... a computer won't necessarily support all valid code pages...?

Right. And fonts won't support all possible characters (I'm aware of only one project were developers try to achieve this).

It's important to understand the difference between files-charset vs. window-font.
A file consists of bits and bytes. You need a translation between the bytes read (or written) and the characters that shall be represented by these bytes. The charset (code page) is just the rule for this translation.
A window needs a font for graphically displaying these characters. Even if the translation of the bytes read succeed the characters can't be displayed if the font don't support them (that's what happened to Saso).


Yes -- I understand that, at least. That sounds like the final word on this, then! So it is literally impossible to guarantee that anything other than 7-bit ASCII characters will appear on screen correctly?

I thought the solution in your first reply might work. But is sounds like this needs a known font to be specified. And it looks like you can't do this from within a batch file.

But... my solution of writing the batch file in UTF-8 without a BOM to echo UTF-8 characters to a text file is pretty reliable, right?

--------

It's a long-shot, but... Would there be any way to get a batch file to open a new console window with a specific font and code-page, and then re-direct commands to that window, thus guaranteeing that the first solution you posted would work every time...? :-/

I guess I might just have to make do with ASCII on screen!

Thanks again for everyone's really helpful replies. I feel like I've learned so much! :-)

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

Re: Box-drawing characters in batch files...?

#19 Post by aGerman » 10 Feb 2017 11:57

esuhl wrote:Going back to your example, let's say I want to echo the õ character to screen. Notepad on your system uses a different encoding, so you enter the character as ä, but doing chcp 850 will (I thought) make sure the character appears correctly on all systems...?

So long as the batch file contains byte E4, and the console is set to chcp 850... I don't understand why this wouldn't work...? (Other than the console font not having such a character.)

This will work as long as the byte is E4. But whether or not letter ä results in the byte E4 depends on the code page used by the text editor :wink:

esuhl wrote:Yes -- I understand that, at least. That sounds like the final word on this, then! So it is literally impossible to guarantee that anything other than 7-bit ASCII characters will appear on screen correctly?

If you have to make sure that everything will be displayed correctly the answer is yes. Every single-byte code page supports ASCII and every text font, too.
Althoug out of my experiences every OEM code page and every console font supports at least the single-frame characters.


esuhl wrote:But... my solution of writing the batch file in UTF-8 without a BOM to echo UTF-8 characters to a text file is pretty reliable, right?

It should also let the characters appear on the screen if you change the code page to 65001 using CHCP.

esuhl wrote:Would there be any way to get a batch file to open a new console window with a specific font and code-page, and then re-direct commands to that window, thus guaranteeing that the first solution you posted would work every time...? :-/

I should rather answer with a no. Third party tools may be able to change the font. Also changing the registry settings may work (but you have to make sure to reset them). But you never know if the font you need would be even installed. E.g. I remember that Consolas was missing on my XP machine.

Steffen

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Box-drawing characters in batch files...?

#20 Post by penpen » 10 Feb 2017 16:21

If you know how to program in c, then you could load fonts temporarily for one windows session, just like carlos did it here:
http://consolesoft.com/p/bg/bg_src/pixelfnt.c.

The resulting application "pixelfnt.exe":
http://www.dostips.com/forum/viewtopic.php?f=3&t=6254.

It works from Win XP to Win 8.1; maybe it also works under win10, but i haven't tested the pixelfnt there.


penpen

Aspidiske
Posts: 7
Joined: 30 Oct 2016 09:48

Re: Box-drawing characters in batch files...?

#21 Post by Aspidiske » 02 Sep 2017 15:54

I made a script to change the font, it works here, it makes the change through the registry, I tested on windows 7 and 10 using chcp 865
my doubt is if it works in other languages

File name: Test.bat

Code: Select all

echo off
color f
reg Query HKCU\Console\%%SystemRoot%%_system32_cmd.exe /v FaceName>>temp.dat
for /f "usebackq skip=2 tokens=1,2,3* delims=" %%a in  (temp.dat) do set valuereg=%%a
set valuereg=%valuereg:~26,15%
Del /f /q /a temp.dat >nul 2>&1
Rem ########### changing font ###########
if not "%valuereg%"=="Lucida Console" (
reg add HKCU\Console\%%SystemRoot%%_system32_cmd.exe /V ScreenColors /t REG_DWORD /d "0x0000000f" /f
reg add HKCU\Console\%%SystemRoot%%_system32_cmd.exe /V ScreenBufferSize /t REG_DWORD /d "0x00190050" /f
reg add HKCU\Console\%%SystemRoot%%_system32_cmd.exe /V FaceName  /t REG_SZ /d "Lucida Console" /f
reg add HKCU\Console\%%SystemRoot%%_system32_cmd.exe /V FontSize  /t REG_DWORD /d "0x000c0000" /f
reg add HKCU\Console\%%SystemRoot%%_system32_cmd.exe /V FontFamily  /t REG_DWORD /d "0x00000036" /f
reg add HKCU\Console\%%SystemRoot%%_system32_cmd.exe /V FontWeight  /t REG_DWORD /d "0x00000190" /f
start Test.bat
exit
)
set valuereg=
chcp 865
cls
mode 80,25
echo.Draw boxes with single-frame characters.                                       
echo. ÚÄÂÄ¿                                                                         
echo. ³A³B³                                                                         
echo. ÃÄÅÄ´                                                                         
echo. ³C³D³                                                                         
echo. ÀÄÁÄÙ                                                                         
echo.Draw boxes with double-frame characters.                                       
echo. ÉÍËÍ»                                                                         
echo. ºAºBº                                                                         
echo. ÌÍÎ͹                                                                         
echo. ºCºDº                                                                         
echo. ÈÍÊͼ                                                                         
echo.1st combination (single vertical, double horizontal frames).                   
echo. ÕÍÑ͸                                                                         
echo. ³A³B³                                                                         
echo. ÆÍØ͵                                                                         
echo. ³C³D³                                                                         
echo. ÔÍÏ;                                                                         
echo.2nd combination (double vertical, single horizontal frames).                   
echo. ÖÄÒÄ·                                                                         
echo. ºAºBº                                                                         
echo. ÇÄ×Ķ                                                                         
echo. ºCºDº                                                                         
echo. ÓÄÐĽ                                         
pause >NUL
Attachments
boxok1.PNG
boxok1.PNG (12.19 KiB) Viewed 10526 times
Last edited by Aspidiske on 02 Sep 2017 17:33, edited 1 time in total.

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

Re: Box-drawing characters in batch files...?

#22 Post by aGerman » 02 Sep 2017 16:49

Some notes

1) You don't reset the registry updates which leads to permanent changes of the console settings.
2) The code doesn't display what it should because you don't use the right characters in the combinated single/double frames. (Compare it with my screenshot below, created with the code I provided in my first reply.)
3) It only works if the the ANSI codepage (used by your text editor) is set to Windows-1252.

Please read the whole thread to understand this behavior.

Steffen

box.png
box.png (14.89 KiB) Viewed 10530 times

Aspidiske
Posts: 7
Joined: 30 Oct 2016 09:48

Re: Box-drawing characters in batch files...?

#23 Post by Aspidiske » 02 Sep 2017 17:47

aGerman wrote:Some notes

1) You don't reset the registry updates which leads to permanent changes of the console settings.
2) The code doesn't display what it should because you don't use the right characters in the combinated single/double frames. (Compare it with my screenshot below, created with the code I provided in my first reply.)
3) It only works if the the ANSI codepage (used by your text editor) is set to Windows-1252.

Please read the whole thread to understand this behavior.

Steffen

box.png


You're right agerman the boxes code was wrong, I do not know how I did it
I already corrected the post! :roll:
OK I'll give a better view in the post

RaceQuest
Posts: 6
Joined: 02 Feb 2016 22:19

Re: Box-drawing characters in batch files...?

#24 Post by RaceQuest » 29 Jan 2018 14:29

esuhl wrote:
04 Feb 2017 22:34
But... I tried using Notepad++ and can paste and save a file with a box-character in it. The status bar shows that the file is encoded in UTF-8, but (unlike in Windows Notepad) the batch file saves and runs fine... except the box-characters echoed to the screen appear as capital accented characters. (The ones output to the text file appear fine.)
For anyone else puzzled why they don't see box characters in a file in Notepad++ (tested in v6.7.7) ensure you select menu Encoding|Character Sets|Western European|OEM-US to see box characters correctly. My Notepad++ was starting with default to character set TIS-620 yours most likely is set to "ANSI". Saving in "UTF-8" /" UTF-8 without BOM" or ANSI still preserves the file data OEM-US setting just means you can see it properly. If you use the "ASCII Panel" sidebar they will insert correctly but side bar (I guess displays in codepage 850) does not show them correctly in character column. Don't confuse this with the I never set it to that and have no idea why it selected Thai character set. You only have notice a difference when characters higher than ASCII 127 are in file.
Last edited by RaceQuest on 29 Jan 2018 15:07, edited 2 times in total.

RaceQuest
Posts: 6
Joined: 02 Feb 2016 22:19

Re: Box-drawing characters in batch files...?

#25 Post by RaceQuest » 29 Jan 2018 14:43

I had two very similar machines but one started cmd.exe using codepage 850 while the other one defaulted to cmd.exe using codepage 437. After reading the thread,some experimenting and several reboots I found "Language for non-Unicode programs" setting caused the difference:
  • "English (US)" gave cmd.exe using codepage 437
  • "English (Australia)" gave cmd.exe using codepage 850
Image
The setting is in control panel|Regional and Language options|Advanced tab. I have no idea why it makes a difference (in Win XP) between two variation of "English" and I don't think it is all that obvious. Is this behavior the same on Win 7 or higher?

As you can see in this thread there are a number of things that have to be set correctly to edit and display the box characters the way the MS-DOS ancients used to behave ;-)

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

Re: Box-drawing characters in batch files...?

#26 Post by aGerman » 29 Jan 2018 16:41

Just execute CHCP if you want to figure out what your OEM code page defaults to. Obviously CP 437 and 850 are not the same.
https://en.wikipedia.org/wiki/Code_page_437
https://en.wikipedia.org/wiki/Code_page_850
CP 437 is mainly used in the US while a lot of other "Latin" environments (including English environments like the UK and Australia) normally use CP 850 in console applications.

Steffen

Post Reply