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-06-24 05:13
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to align text with bat? View 5,488 Replies 30
Original Poster Posted 2006-12-13 07:15 ·  中国 贵州 贵阳 电信
中级用户
★★
Credits 304
Posts 117
Joined 2006-04-04 18:43
20-year member
UID 53325
Gender Male
Status Offline
New Compilation of Essentials for TCM Clinical Practice
Author: Lu Xiangzhi  SS Number: 10804820  Call Number: R24/15

Basic Theory of TCM Clinical Medicine
Author: Hunan College of TCM  SS Number: 10806223  Call Number: R24/1

Essential Discussions on Clinical Syndrome Treatment
Author: Xu Chunjia, Qin Qun  SS Number: 10806254  Call Number: R24/152

[ Last edited by wydos on 2006-12-13 at 07:17 AM ]
Floor 2 Posted 2006-12-13 07:19 ·  中国 河南 郑州 电信
中级用户
★★
Credits 439
Posts 170
Joined 2006-01-09 20:29
20-year member
UID 48707
Status Offline
Vaguely remember that there is one that can make TAB tab. It seems to be 8 spaces.
bat c c++
Floor 3 Posted 2006-12-13 07:35 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
We need to count the maximum length of each section of the string to facilitate typesetting.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-12-13 07:53 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  I don't know if WMIC can do it. Just ask the moderator and you'll know.
Floor 5 Posted 2006-12-13 08:03 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Personally, purely using P is difficult to implement because it's hard to judge the number of bytes of variables, so we need to rely on vbs.

::On Error Resume Next
echo ' > nul &cls & @echo off
echo ' > nul &for /f "tokens=1,2,3,4 delims= " %%i in (test.txt) do (
echo ' > nul &cscript //E:vbs "%~f0" "%%i" "%%j" "%%k" "%%l" //nologo)
echo ' > nul &pause>nul

::str0=wscript.arguments(0)
::str1=wscript.arguments(1)
::str2=wscript.arguments(2)
::str3=wscript.arguments(3)
::wscript.echo align(str0)&align(str1)&align(str2)&align(str3)
::function align(obj)
:: length=len(obj)
:: flag=0
:: for i=1 to length
:: str=mid(obj,i,1)
:: str=asc(str)
:: if str<0 then
:: flag=flag+1
:: end if
:: next
:: length=20-length-flag
:: align=obj&space(length)
::end function


Description: Save the text to be processed as test.txt, and save this code as: ok.bat

[ Last edited by youxi01 on 2006-12-13 at 08:14 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +7 2006-12-13 08:19
Floor 6 Posted 2006-12-13 09:15 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline

I don't know if WMIC can do it. Just ask the moderator and you'll know.


Sweat, WMIC is used to handle text~~~

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 7 Posted 2006-12-13 12:09 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
```
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-4 delims=Author: SS number: Call number: " %%i in (test.txt) do (
set a=&set b=&set c=
call :detect %%i
set /a num=2*10-2*num
call :add %%i
set a=!i!
call :detect %%j
set /a num=2*8-2*num
call :spa %%j
set /a num+=!sp!
call :add %%j
set b=!i!
call :detect %%k
set /a num=12-num
call :add %%k
set c=!i!
echo !a!Author: !b!SS number: !c!Call number: %%l>>array.txt
)
start array.txt
goto :eof

:detect
set x=%*◎
set num=0
:detect_
if not "%x:~0,1%"=="◎" (
set /a num+=1
set "x=%x:~1%"
goto :detect_
)
goto :eof

:add
set y=%*
for /l %%i in (1,1,%num%) do set y=!y!
set i=%y%
goto :eof

:spa
set z=%*◎
set sp=0
:spa_
if not "%z:~0,1%"=="◎" (
if "%z:~0,1%"==" " set /a sp+=1
set "z=%z:~1%"
goto :spa_
)
goto :eof
```

Only tested the three lines you provided, and other formats and special characters were not considered. Please note the difference between full-width and half-width colons.

[ Last edited by pengfei on 2006-12-13 at 12:53 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
无奈何 +4 2006-12-14 02:33
redtek +5 2006-12-14 21:35
业精于勤而荒于嬉,形成于思而毁于随。
Floor 8 Posted 2006-12-13 14:30 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
“delims=Author: SS number: Call number:” has a shortcoming. As long as there is one of these characters in a certain position, it will cause a loss.

You can first consider replacing "Author:", "SS number:", "Call number:" with $ and then handle it.

Highlight: Detect byte count
>Temp.txt set/p=!Temp_%1!<nul
for %%i in (Temp.txt) do set %1=%%~zi



@echo off
setlocal enabledelayedexpansion
cd.>array.txt
::Detect the maximum number of bytes in each segment
for /f "delims=" %%i in (test.txt) do (
set "Temp$=%%i"
set "Temp$=!Temp$:Author:=$!"&&set "Temp$=!Temp$:SS number:=$!"&&set "Temp$=!Temp$:Call number:=$!"
for /f "tokens=1-4 delims=$" %%i in ("!Temp$!") do (
set "Temp_=%%i"&&call :MaxNum Book title
set "Temp_=%%j"&&call :MaxNum Author
set "Temp_=%%k"&&call :MaxNum SS number
set "Temp_=%%l"&&call :MaxNum Call number
))

::Add spaces for alignment
for /f "delims=" %%i in (test.txt) do (
set "Temp$=%%i"
set "Temp$=!Temp$:Author:=$!"&&set "Temp$=!Temp$:SS number:=$!"&&set "Temp$=!Temp$:Call number:=$!"
for /f "tokens=1-4 delims=$" %%i in ("!Temp$!") do (

set "Temp_Book title=%%i"&&call :Num Book title
set /a Book title=%MaxBook title%-!Book title!&&call :Add Book title

set "Temp_Author=%%j"&&call :Num Author
set /a Author=%MaxAuthor%-!Author!&&call :Add Author

set "Temp_SS number=%%k"&&call :Num SS number
set /a SS number=%MaxSS number%-!SS number!&&call :Add SS number

set "Temp_Call number=%%l"&&call :Num Call number
set /a Call number=%MaxCall number%-!Call number!&&call :Add Call number

>>array.txt echo.!Temp_Book title!Author:!Temp_Author!SS number:!Temp_SS number!Call number:!Temp_Call number!
))
del Temp.txt
pause
exit

::Detect byte count
:MaxNum
>Temp.txt set/p=%Temp_%<nul
for %%i in (Temp.txt) do if %%~zi GTR !Max%1! set Max%1=%%~zi
goto :eof

::Detect byte count
:Num
>Temp.txt set/p=!Temp_%1!<nul
for %%i in (Temp.txt) do set %1=%%~zi
goto :eof

::Add number of spaces
:Add
if "!%1!" == "0" goto :eof
set "Temp_%1=!Temp_%1! "
set /a %1-=1
goto Add


[ Last edited by zh159 on 2006-12-13 at 09:35 AM ]
Recent Ratings for This Post ( 5 in total) Click for details
RaterScoreTime
无奈何 +8 2006-12-14 02:33
youxi01 +4 2006-12-14 03:06
pengfei +5 2006-12-14 04:30
kcdsw +4 2006-12-14 06:39
redtek +5 2006-12-14 21:35
Floor 9 Posted 2006-12-13 21:53 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
It's all great. I've always been not good at using the FOR statement. I didn't expect the FOR statement to be so useful. Learned it.
Floor 10 Posted 2006-12-13 22:14 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,179
Posts 442
Joined 2006-09-09 22:47
19-year member
UID 62249
Status Offline
Originally posted by youxi01 at 2006-12-13 08:03:
In my opinion, pure P is difficult to implement because P is difficult to judge the number of bytes of variables, so it is necessary to rely on vbs.

::On Error Resume Next
echo ' > nul &cls & @echo off
echo ' > nul & ...



Ask, :: is the same as REM, which is a comment? Why is the operation abnormal after removing ::? Strange, can the building owner explain it.
Floor 11 Posted 2006-12-14 01:18 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Because :: can be recognized (identified as a line break) in VBS, while rem cannot be recognized by VBS!
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HUNRYBECKY +2 2006-12-14 03:40
Floor 12 Posted 2006-12-14 01:22 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Question from user 7F: If there are E characters or numbers in the book title, how to solve it?
What an excellent idea by user 8F to judge the number of bytes by the size of the file!
Floor 13 Posted 2006-12-14 01:51 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Aligning or first supplementing and then splitting is the most convenient.
Floor 14 Posted 2006-12-14 02:31 ·  中国 浙江 宁波 鹏博士宽带
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Brother zh159, the use of byte count to determine length in building 8 is indeed a highlight. In CMD, both Chinese characters and English letters in a string have a length of 1, and using byte judgment well solves this problem. However, special characters in CMD are really a headache, and I have not found a good method all the time.

This post is a very typical problem of formatted output. Most languages' printf can solve it well.

You can try the following awk script:

Usage: gawk -f script.awk file.txt


BEGIN{
FS=" +(作者|号|索书号):"
}
{
L1=L1<length($1)?(length($1)):L1
L2=L2<length($2)?(length($2)):L2
L3=L3<length($3)?(length($3)):L3
L4=L4<length($4)?(length($4)):L4
for (i=1;i<=NF;i++)
R=$i
}
END{
format = "%-"L1"s 作者:%-"L2"s SS号:%-"L3"s 索书号:%-"L4"s\n"
for(i=1;i<=NR;i++)
if(R != "") printf format, R,R,R,R
}


gawk download: http://www.klabaster.com/progs/gawk32.zip

[ Last edited by 无奈何 on 2006-12-14 at 03:23 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-12-14 21:36
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 15 Posted 2006-12-14 02:44 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
I'm sorry I'm slow-witted, I can't understand brother's code.
To avoid the suspicion of flattery, I won't give brother points, please understand.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Forum Jump: