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 13:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to display characters with inconsistent lengths with a fixed length? View 2,085 Replies 2
Original Poster Posted 2006-05-21 13:58 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In order to make the typesetting beautiful, you need to display several lines of characters with inconsistent lengths in a fixed length. How to achieve it?

The code is as follows:


@echo off
cls
color 2e
title Disk partition remaining space situation
echo.
echo.
echo ╔══════════════════════════════╗
echo ║ Disk partition Remaining space (MB) ║
echo ║ _____________________________________________________ ║
setlocal enabledelayedexpansion
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%a:\nul (
for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "Available bytes"') do (set freesize=%%b
set /a freesize=!freesize:~0,-3!/1049>nul 2>nul
echo ║ %%a: !freesize! ║
)
)
)
echo ╚══════════════════════════════╝
pause>nul


[ Last edited by willsort on 2006-5-22 at 00:33 ]
Floor 2 Posted 2006-05-21 19:18 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
In my opinion, I'm afraid some third-party tools are needed. At least, they can calculate the length of strings.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2006-05-22 11:39 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline

───────────────── Moderation Record ─────────────────
Performed by: Will Sort
Action: Move Topic: From "DOS Troubleshooting & Question Discussion (Help Desk)"
Description: Classified according to the topic content, more suitable for posting in this forum area
Reminder: The original area transfer link will be deleted after three days
Punishment: Deduct 6 points of points awarded for posting this topic, and deduct 2 points of points for forum area violations
───────────────── Moderation Record ─────────────────

Re namejm:

Calculating the length of a string in CMD is not very complicated. It can be achieved using for + set. The specific algorithm can be referred to . Then, according to specific needs, the string can be padded with spaces to a certain length.

However, there is a simpler solution for your requirement. Assuming that the length range of the string freesize is 0~17, then we first pad 17 spaces in front of freesize, and then directly take the last 17 characters of the string, which can get the right-aligned number freesize.

However, I still suggest that not using a border is the simplest way to handle typesetting.


@echo off
cls
color 2e
title Disk Partition Free Space Status
echo.
echo.
echo ╔══════════════════════════════╗
echo ║ Disk Partitions Free Space (MB) ║
echo ║ _____________________________________________________ ║
setlocal enabledelayedexpansion
for %%a in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%a:\nul (
for /f "tokens=3" %%b in ('dir /-c %%a:\^|find "Available bytes"') do (
set freesize=%%b
set /a freesize=!freesize:~0,-3!/1049>nul 2>nul
set freesize= !freesize!
echo ║ %%a: !freesize:~-17! ║

)
)
)
echo ╚══════════════════════════════╝
pause>nul


Batch Code for Calculating String Length for Error Checking
http://www.cn-dos.net/forum/viewthread.php?tid=19819

[ Last edited by willsort on 2006-5-23 at 00:25 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: