───────────────── 版务记录 ─────────────────
执行:Will Sort
操作:移动主题:自《DOS疑難解答 & 問題討論 (解答室)》
说明:依照主题内容分类,更适合于发表在此版区
提示:原版区转向链接三日后将删除
处罚:扣除因发表该主题而奖励的6点积分,版区违规惩罚性扣除2点积分
───────────────── 版务记录 ─────────────────
Re namejm:
在CMD下,计算字符串长度不是很复杂,利用for+set可以实现,具体算法可以参考。之后,可以根据具体需要,对字符串用空格补齐长度。
不过,对于你的需求有更简单的方案。假设字符串freesize长度范围为0~17,则我们首先在freesize前补足17个空格,然后直接取字符串的后17个字符,即可得到右对齐排列的数字freesize。
不过,我仍然建议你,不使用边框是最简单的排版处理办法。
@echo off
cls
color 2e
title 各分区剩余空间情况
echo.
echo.
echo ╔══════════════════════════════╗
echo ║ 磁盘分区 剩余空间(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 "可用字节"') do (
set freesize=%%b
set /a freesize=!freesize:~0,-3!/1049>nul 2>nul
set freesize= !freesize!
echo ║ %%a: !freesize:~-17! ║
)
)
)
echo ╚══════════════════════════════╝
pause>nul
计算字符串长度批处理代码查错
http://www.cn-dos.net/forum/viewthread.php?tid=19819
Last edited by willsort on 2006-5-23 at 00:25 ]
───────────────── 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 ]