Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box drawing characters in batch scripts (Windows CMD)

I would like to display box characters (single o double line)in batch scripts that are aim to run on Windows CMD environments (XP,7,8 and reactOS). These symbols for "boxes" are specified in code page 1252.

enter image description here

From script I am setting the necessary code 850 or 437 with CHCP command.

chcp 437

for writing I am Using ECHO command

ECHO "char to display"

What file encoding should i use (ANSI, UTF8,..)?

like image 940
Daniel Perez Avatar asked Oct 16 '22 09:10

Daniel Perez


1 Answers

Open a command prompt, run chcp (change code page) without any parameter and the Windows command processor outputs the code page of the character encoding expected by cmd.exe on interpreting a batch file according to country configured for the user account used to execute the batch file.

However, it is possible to use for example chcp 437 >nul to set explicitly the code page 437 before the batch file outputs characters with command echo. In this case all characters in the batch file should be encoded with using code page 437. Code page 437 is used by default in North American countries (Canada, USA) and for that reason supported by all fonts used by default for Windows console windows.

Another very common code page used for Windows console is code page 850 being similar to code page 437, but has less box drawing characters in comparison to code page 437. This code page is used by default in Western European countries. It is also supported by all fonts used by default for Windows console windows.

The two referenced Wikipedia pages about the code pages 437 and 850 show the box drawing characters and their decimal and hexadecimal code values on being encoded with one byte per character, i.e. using "ANSI" encoding. "ANSI" is not really a correct term here because the code pages 437 and 850 are OEM code pages which are not standardized by the American National Standards Institute (ANSI). But Microsoft used the term ANSI for all charater encodings using just one byte per character.

The Wikipedia pages about the code pages 437 and 850 show also the Unicode code value in case of UTF-8 encoding is used for the batch file. But please be aware that some fonts used by default for Windows console window like Terminal (raster font) used by default on Windows 7 does not support UTF-8 encoding. For details see my answer on Using another language (code page) in a batch file made for others and the comments below the answer.

I recommend to use "ANSI" or more precise OEM character encoding for the batch file with echo command lines which output box drawing characters encoded with code page 437.

The "ANSI" encoding used by default by Windows GUI text editors for countries in North America and Western Europe is Windows-1252. This could be important to know if the used text editor does not support displaying the batch file content with interpreting the bytes using code page 437 and for that reason it is necessary to enter the Windows-1252 characters with the code values which result in displaying the box drawing characters on being interpreted with OEM code page 437.

Some editors like UltraEdit support displaying a one byte per character encoded text file with any code page as long as the configured font supports also this code page.

The font Terminal is definitely a good choice as text editor font on writing a batch file which should output box drawing characters.

Example:

A batch file contains following command lines OEM encoded with code page 437:

@echo off
%SystemRoot%\System32\chcp.com 437 >nul
echo ┌───────────────┐
echo │ box drawing 1 │
echo └───────────────┘
echo/
echo ╔═══════════════╗
echo ║ box drawing 2 ║
echo ╚═══════════════╝

This batch file contains following bytes (offset: hexadecimal bytes ; ASCII representation):

0000h: 40 65 63 68 6F 20 6F 66 66 0D 0A 25 53 79 73 74 ; @echo off..%Syst
0010h: 65 6D 52 6F 6F 74 25 5C 53 79 73 74 65 6D 33 32 ; emRoot%\System32
0020h: 5C 63 68 63 70 2E 63 6F 6D 20 34 33 37 20 3E 6E ; \chcp.com 437 >n
0030h: 75 6C 0D 0A 65 63 68 6F 20 DA C4 C4 C4 C4 C4 C4 ; ul..echo ÚÄÄÄÄÄÄ
0040h: C4 C4 C4 C4 C4 C4 C4 C4 C4 BF 0D 0A 65 63 68 6F ; ÄÄÄÄÄÄÄÄÄ¿..echo
0050h: 20 B3 20 62 6F 78 20 64 72 61 77 69 6E 67 20 31 ;  ³ box drawing 1
0060h: 20 B3 0D 0A 65 63 68 6F 20 C0 C4 C4 C4 C4 C4 C4 ;  ³..echo ÀÄÄÄÄÄÄ
0070h: C4 C4 C4 C4 C4 C4 C4 C4 C4 D9 0D 0A 65 63 68 6F ; ÄÄÄÄÄÄÄÄÄÙ..echo
0080h: 2F 0D 0A 65 63 68 6F 20 C9 CD CD CD CD CD CD CD ; /..echo ÉÍÍÍÍÍÍÍ
0090h: CD CD CD CD CD CD CD CD BB 0D 0A 65 63 68 6F 20 ; ÍÍÍÍÍÍÍÍ»..echo 
00a0h: BA 20 62 6F 78 20 64 72 61 77 69 6E 67 20 32 20 ; º box drawing 2 
00b0h: BA 0D 0A 65 63 68 6F 20 C8 CD CD CD CD CD CD CD ; º..echo ÈÍÍÍÍÍÍÍ
00c0h: CD CD CD CD CD CD CD CD BC 0D 0A                ; ÍÍÍÍÍÍÍͼ..

The ASCII representation of the bytes use code page Windows-1252. So it can be seen here how same byte value can result in a different character being displayed just because of using a different code page of "ANSI" encoded text file.

The same batch file would contain UTF-8 encoded without byte order mark:

0000h: 40 65 63 68 6F 20 6F 66 66 0D 0A 25 53 79 73 74
0010h: 65 6D 52 6F 6F 74 25 5C 53 79 73 74 65 6D 33 32
0020h: 5C 63 68 63 70 2E 63 6F 6D 20 34 33 37 20 3E 6E
0030h: 75 6C 0D 0A 65 63 68 6F 20 E2 94 8C E2 94 80 E2
0040h: 94 80 E2 94 80 E2 94 80 E2 94 80 E2 94 80 E2 94
0050h: 80 E2 94 80 E2 94 80 E2 94 80 E2 94 80 E2 94 80
0060h: E2 94 80 E2 94 80 E2 94 80 E2 94 90 0D 0A 65 63
0070h: 68 6F 20 E2 94 82 20 62 6F 78 20 64 72 61 77 69
0080h: 6E 67 20 31 20 E2 94 82 0D 0A 65 63 68 6F 20 E2
0090h: 94 94 E2 94 80 E2 94 80 E2 94 80 E2 94 80 E2 94
00a0h: 80 E2 94 80 E2 94 80 E2 94 80 E2 94 80 E2 94 80
00b0h: E2 94 80 E2 94 80 E2 94 80 E2 94 80 E2 94 80 E2
00c0h: 94 98 0D 0A 65 63 68 6F 2F 0D 0A 65 63 68 6F 20
00d0h: E2 95 94 E2 95 90 E2 95 90 E2 95 90 E2 95 90 E2
00e0h: 95 90 E2 95 90 E2 95 90 E2 95 90 E2 95 90 E2 95
00f0h: 90 E2 95 90 E2 95 90 E2 95 90 E2 95 90 E2 95 90
0100h: E2 95 97 0D 0A 65 63 68 6F 20 E2 95 91 20 62 6F
0110h: 78 20 64 72 61 77 69 6E 67 20 32 20 E2 95 91 0D
0120h: 0A 65 63 68 6F 20 E2 95 9A E2 95 90 E2 95 90 E2
0130h: 95 90 E2 95 90 E2 95 90 E2 95 90 E2 95 90 E2 95
0140h: 90 E2 95 90 E2 95 90 E2 95 90 E2 95 90 E2 95 90
0150h: E2 95 90 E2 95 90 E2 95 9D 0D 0A

Note: The font used by your browser to display the batch file code above could result in getting the two boxes not displayed as real closed boxes with same width on all six lines as it does in a Windows console window of Windows XP and Windows 7 with default raster font or with font Lucida Console which is by default also available in properties of a Windows console window. Lucida Console supports much more characters than Terminal, but it is not the default font for console windows.

The text editor UltraEdit has an ASCII Table view for which the font Terminal can be set which is an OEM font. This makes it very easy to enter the box drawing characters which are displayed in ASCII Table view with font Terminal and which can be inserted into the batch file with double clicking on these characters in the view.

like image 53
Mofi Avatar answered Oct 18 '22 15:10

Mofi