中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-26 02:18
47,812 topics / 349,910 posts / today 0 new / 48,264 members
DOS批处理 & 脚本技术(批处理室) » How to display characters with inconsistent lengths with a fixed length?
Printable Version  2,217 / 2
Floor1 namejm Posted 2006-05-21 13:58
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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:



[ Last edited by willsort on 2006-5-22 at 00:33 ]
Floor2 Climbing Posted 2006-05-21 19:18
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
In my opinion, I'm afraid some third-party tools are needed. At least, they can calculate the length of strings.
Floor3 willsort Posted 2006-05-22 11:39
元老会员 Posts 1,512 Credits 4,432

───────────────── 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 ]
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023