|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『楼 主』:
[已结] 批处理菜单对齐问题。。。(即画表格的问题)
使用 LLM 解释/回答一下
各位大侠,大家是否有过如下的经历呢~!
在制作批处理表格的时候,若里面有变量的存在,则这样一个表格在编写的时候总是对不齐的。。因为变量有可能大或小。。
所以,我们在编写表格的时候,总是尽量避免在表格中使用变量,但一个好的批处理,若是因为如此就放弃了在表格中的变量使用,未免有点可惜了。
怕说得不够明白:举个例子。
假设现在有几个变量。
set /p name=
set /p add=
set /p phone=
我需要在屏幕上画一个表格。。
┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ %name% %phone% ┃
┃ %add% ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
就如此粗略表示一下,,那么大家在遇到这样的情况下的时候,是如何处理后面的线与上方对齐的问题的呢?
个人有个不成熟的想法。。但在处理中文和非英文的问题上遇到了问题。。
已知:制表格的线占两个字符,汉字占一个字符。英文和各类符号占用一个字符。
若变量全为英文或汉字,则该想法无问题。。
---------------------------------------------------------------------------------------------------------
1,假设我一行要画37个表格线。就是74个字符的距离。其中表格会用去4个字符。
2,变量所占用的空间,使用 for /l %%i in (0,1,1000) 来获取变量的字符数量。使该字符数不大于35 或 70。
3,然后利用 35 或 70 减去这个数量。获得需要画上的空格数量就得到了表格边线的距离答案。
---------------------------------------------------------------------------------------------------------
但,经常性的。我们写的变量是由中文+英文的结果。那么这种情况下如何正确的获得该变量的真实字符数。
呵,愿大家一起集思广益。解决这个难题,也愿大家画出比较好看的菜单(表格)出来。
谢谢 youxi01 ,lxmxn 。让我学到了新的知识。。
一个取文件大小,一个字符判断,(对assic修改一下估计就没有问题了)
Last edited by flyinspace on 2007-4-22 at 06:12 PM ]
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 ]
|

知,不觉多。不知,乃求知 |
|
2007-4-23 02:52 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
   『第 2 楼』:
使用 LLM 解释/回答一下
对于中英文 结合的 变量的对齐,我们一般采用的做法当然是先要取得该变量的总字节数(这个工作一般由vbs来完成,当然 willsort似乎也写过用P处理的),然后再来对齐。
以下程序也用纯批处理来获取字节,不过在此没考虑特殊字符。原理很简单:如果某字符 大于 “Z” ,则认为它为汉字(同样,不处理特殊字符)。
欢迎测试,指正。
@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
)
测试结果如下:
┍━━━━━━━━━━━━━━━━━━━━━━━┓
┃姓名 地址 电话 ┃
┃ ┃
┃dos china123 521 ┃
┃dos联盟 En中国EN 没有12 ┃
┃联盟 中国 测试 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┛
将前面的内容,再换掉,测试如下:
┍━━━━━━━━━━━━━━━━━━━━━━━┓
┃姓名 地址 电话 ┃
┃ ┃
┃dos我爱你 china123 521我爱她 ┃
┃dos联盟 En中国EN 没有12 ┃
┃女神,I love U 中国HOHO12 测试ENNDFNAS ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┛
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 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┛
|
|
2007-4-23 04:47 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
  『第 3 楼』:
使用 LLM 解释/回答一下
按照兄的要求写的,可自行修改。
也请大家测试。
@echo off&SetLocal EnableDelayedExpansion
set /p name=请输入姓名:
set /p phone=请输入电话:
set /p add=请输入地址:
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 ]
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 ]
此帖被 +20 点积分 点击查看详情 评分人:【 bjsh 】 | 分数: +4 | 时间:2007-4-23 05:31 | 评分人:【 flyinspace 】 | 分数: +8 | 时间:2007-4-23 06:57 | 评分人:【 htysm 】 | 分数: +8 | 时间:2007-4-24 00:23 |
|
附件
1: BatchMemu.JPG (2007-4-23 05:04, 27.09 KiB, 下载附件所需积分 1 点
,下载次数: 4)
|
|
2007-4-23 05:04 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
lxmxn兄写的漂亮就是空格还是不够长;再加长点;
你的试试:姓名:1电话:1地址:1;会出现异常;加长space;
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;
|
|
2007-4-23 05:33 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Re bjsh:
多谢兄测试指出不足。代码已修正。
Last edited by lxmxn on 2007-4-22 at 04:41 PM ]
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 ]
|
|
2007-4-23 05:39 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
谢谢二楼和三楼的方案。。收益非浅。。
二楼的算法思想是:判断字符是否大于二。
而三楼的则是写入文件。。
在此有个问题。。
>temp.tem<nul set/p=%string%
这个 写日文件方案可以解释一下么??
不知道原来还可以这样用的。
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.
|

知,不觉多。不知,乃求知 |
|
2007-4-23 07:07 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
>temp.tem<nul set/p=%string%
相当于
set /p=%sring% <nul >temp.tem
set /p=%sring% <nul 这个是 在控制台上输出 %string%;
定向到temp.tem 不换行;
通常 set /p 变量名=提示字符串:
来获取输入;在这里用<nul来赋予一个空字符串;
效果就是屏蔽用户输入;只显示提示字符串;常用来输出不换行;此时变量名可省
Last edited by bjsh on 2007-4-22 at 06:29 PM ]
>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 ]
|
|
2007-4-23 07:27 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
lxmxn兄就是利用这个把不含回车符的字符串重定向到文件;
再利用%~za来获取字节数;
这时候当然就没有汉字与字母之分了
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
|
|
2007-4-23 07:29 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by flyinspace at 2007-4-22 18:07:
在此有个问题。。
>temp.tem<nul set/p=%string%
这个 写日文件方案可以解释一下么??
不知道原来还可以这样用的。
“>temp.tem<nul set/p=%string%”的作用和“set/p=%string%>temp.tem<nul”相同, 其实就是语句的位置替换了,但是作用都是一样的。
类似的语句还有很多,只要兄认真实践总结,方法很多的。
比如:
>1.txt echo Hello,world.
和
echo Hello,world.>1.txt
的作用相同。
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.
|
|
2007-4-23 07:31 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
原来bjsh已经提前发了,算我灌水了。
So it turns out that bjsh has already posted in advance, so I guess I'm just posting water.
|
|
2007-4-23 07:31 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
首先谢谢两位的回答,
让我知道了原来 ><还可以使用倒装句。。
又多了新的方法了:)
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. : )
|

知,不觉多。不知,乃求知 |
|
2007-4-23 08:44 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
|
2007-4-24 00:07 |
|
|
htysm
高级用户
   
积分 866
发帖 415
注册 2005-12-4
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
九宫格的连接呀。已经看过了,不过偶看不懂。
The connections of the nine-square grid. I've already seen it, but I don't understand it.
|

欢迎你到批处理爱好者联盟QQ群:18023953 |
|
2007-4-24 00:28 |
|
|
flyinspace
银牌会员
    
积分 1206
发帖 517
注册 2007-3-25
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
算法我看懂了?但这个有什么用??
和主题完全没有关系啊?
I understand the algorithm? But what's the use of this?? It has nothing to do with the topic at all!
|

知,不觉多。不知,乃求知 |
|
2007-4-25 08:38 |
|