China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-02 19:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] Batch menu alignment problem... (that is, the problem of drawing tables) View 1,555 Replies 13
Original Poster Posted 2007-04-23 02:52 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Dear experts, have you ever had the following experience~!

When making a batch processing table, if there are variables in it, such a table is always misaligned when writing. Because variables may be large or small.

Therefore, we always try to avoid using variables in the table when writing, but it is a pity to give up using variables in the table if a good batch processing is because of this.

I'm afraid it's not clear enough to say: for example.

Suppose there are several variables now.
set /p name=
set /p add=
set /p phone=
I need to draw a table on the screen.
┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ %name% %phone% ┃
┃ %add% ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Just roughly represent it like this, then how do you deal with the problem of aligning the subsequent line with the top when you encounter such a situation?
Personally, I have an immature idea.. but I encountered a problem in dealing with Chinese and non-English issues.
It is known that the line for making the table occupies two characters, Chinese characters occupy one character, and English and various symbols occupy one character.
If the variables are all English or Chinese characters, then this idea is fine..
---------------------------------------------------------------------------------------------------------
1, Suppose I want to draw 37 table lines in one line, that is, a distance of 74 characters. Among them, the table will use 4 characters.
2, The space occupied by the variable, use for /l %%i in (0,1,1000) to obtain the number of characters of the variable. Make this number of characters not greater than 35 or 70.
3, Then use 35 or 70 minus this number. Obtain the number of spaces to be drawn to get the answer for the distance of the table border line.
---------------------------------------------------------------------------------------------------------
But, frequently, the variables we write are the result of Chinese + English. Then how to correctly obtain the real number of characters of the variable.
Hehe, I hope everyone can pool their ideas. Solve this difficult problem, and also hope everyone can draw a relatively beautiful menu (table) out.


Thank you youxi01, lxmxn. Let me learn new knowledge.

One is to get the file size, and the other is character judgment, (modify the assic a bit, it should be no problem probably)

[ Last edited by flyinspace on 2007-4-22 at 06:12 PM ]
知,不觉多。不知,乃求知
Floor 2 Posted 2007-04-23 04:47 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
For the alignment of variables combining Chinese and English, the general approach we usually adopt is to first obtain the total number of bytes of the variable (this work is generally done by VBS, and of course, willsort seems to have also written one processed by P). Then we proceed with alignment.

The following program also uses pure batch processing to obtain the number of bytes, but special characters are not considered here. The principle is very simple: if a certain character is greater than "Z", it is considered a Chinese character (similarly, special characters are not processed).

Welcome to test and provide corrections.


@echo off
setlocal enabledelayedexpansion

set name1=dos
set add1=china123
set phone1=521

set name2=dos联盟
set add2=En中国EN
set phone2=没有12


set name3=联盟
set add3=中国
set phone3=测试

set "space= "
set str_=┍━━━━━━━━━━━━━━━━━━━━━━━┓
set str1_=┗━━━━━━━━━━━━━━━━━━━━━━━┛
set/a Lnum=15

echo %str_%
echo ┃姓名 地址 电话 ┃
echo ┃ ┃

for /l %%i in (1 1 3) do (
set Tstr=
for %%j in (name add phone) do (

set "str=!%%j%%i!"
call :IsEn

set/a Lnum=%Lnum%-!var!
set str=!str!%space%
call set str=%%str:~0,!Lnum!%%

set Tstr=!Tstr!!str!
)
echo ┃!Tstr! ┃
)

echo %str1_%

pause>nul

:IsEn
set/a var=0
set/a flag=0

for /l %%i in (0,1,1000) do (
set tmp=!str:~%%i,1!
if == goto :eof
set/a flag+=1
if !tmp! GTR z set/a var+=1
)

The test results are as follows:

┍━━━━━━━━━━━━━━━━━━━━━━━┓
┃姓名 地址 电话 ┃
┃ ┃
┃dos china123 521 ┃
┃dos联盟 En中国EN 没有12 ┃
┃联盟 中国 测试 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┛

Replace the previous content and test as follows:

┍━━━━━━━━━━━━━━━━━━━━━━━┓
┃姓名 地址 电话 ┃
┃ ┃
┃dos我爱你 china123 521我爱她 ┃
┃dos联盟 En中国EN 没有12 ┃
┃女神,I love U 中国HOHO12 测试ENNDFNAS ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┛
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
huzixuan +4 2007-04-23 05:05
flyinspace +8 2007-04-23 07:01
Floor 3 Posted 2007-04-23 05:04 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
According to the brother's requirements, it's written. You can modify it by yourself.

Also, please everyone test.


@echo off&SetLocal EnableDelayedExpansion
set /p name=Please enter name:
set /p phone=Please enter phone number:
set /p add=Please enter address:
for %%i in (name phone add) do (call :GetLength "!%%i!" "%%i_len")
set space=
set yes=━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
set /a all_len=72-name_len-phone_len-10
set /a add_lens=72-add_len
set _=echo=
%_%┏%yes%┓
%_%┃%name%%space:~0,10%%phone%!space:~0,%all_len%!┃
%_%┃%add%!space:~0,%add_lens%!┃
%_%┗%yes%┛
del /q/f temp.tem
pause&exit/b

:GetLength
set string=%~1
>temp.tem<nul set/p=%string%
for %%i in (temp.tem) do set %~2=%%~zi
goto :EOF


[ Last edited by lxmxn on 2007-4-22 at 04:39 PM ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
bjsh +4 2007-04-23 05:31
flyinspace +8 2007-04-23 06:57
htysm +8 2007-04-24 00:23
Attachments
BatchMemu.JPG
Floor 4 Posted 2007-04-23 05:33 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Brother lxmxn wrote beautifully, but the spaces are still not long enough; make them longer;

Try yours: Name: 1 Phone: 1 Address: 1; an exception will occur; lengthen the space;
Floor 5 Posted 2007-04-23 05:39 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Re bjsh:

Thanks brother for testing and pointing out the deficiencies. The code has been corrected.

[ Last edited by lxmxn on 2007-4-22 at 04:41 PM ]
Floor 6 Posted 2007-04-23 07:07 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Thanks to the solutions from the second and third floors. It's really rewarding...

The algorithm idea of the second floor is: judge whether the character is greater than two.
And the third floor is writing to a file...

Here's a question...

>temp.tem<nul set/p=%string%

Can you explain this file writing solution? I didn't know it could be used like this originally.
知,不觉多。不知,乃求知
Floor 7 Posted 2007-04-23 07:27 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
>temp.tem<nul set/p=%string%

is equivalent to

set /p=%sring% <nul >temp.tem

set /p=%sring% <nul is to output %string% on the console;

Redirect to temp.tem without line break;

Usually set /p variable name=prompt string:

To get input; here <nul is used to assign an empty string;

The effect is to shield user input; only display the prompt string; often used to output without line break; at this time the variable name can be omitted

[ Last edited by bjsh on 2007-4-22 at 06:29 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +2 2007-04-23 08:34
Floor 8 Posted 2007-04-23 07:29 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Brother lxmxn used this to redirect a string without carriage returns to a file; then used %~za to get the number of bytes; at this time, of course, there is no distinction between Chinese characters and letters
Floor 9 Posted 2007-04-23 07:31 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by flyinspace at 2007-4-22 18:07:
There is a question here..

>temp.tem<nul set/p=%string%

Can you explain this file writing method?

I didn't know it could be used like this before.

The function of ">temp.tem<nul set/p=%string%" is the same as "set/p=%string%>temp.tem<nul", actually it's just the position of the statement is replaced, but the function is the same.
There are many similar statements. As long as you practice and summarize carefully, there are many methods.

For example:
>1.txt echo Hello,world.
and
echo Hello,world.>1.txt
have the same function.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
flyinspace +2 2007-04-23 08:21
Floor 10 Posted 2007-04-23 07:31 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
So it turns out that bjsh has already posted in advance, so I guess I'm just posting water.
Floor 11 Posted 2007-04-23 08:44 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
First, thank you both for your answers, which let me know that the >< can also be used in inverted sentences. Now there's a new method. : )
知,不觉多。不知,乃求知
Floor 12 Posted 2007-04-24 00:07 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 13 Posted 2007-04-24 00:28 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
The connections of the nine-square grid. I've already seen it, but I don't understand it.
欢迎你到批处理爱好者联盟QQ群:18023953
Floor 14 Posted 2007-04-25 08:38 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
I understand the algorithm? But what's the use of this?? It has nothing to do with the topic at all!
知,不觉多。不知,乃求知
Forum Jump: