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-07-01 04:52
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Challenge 6]: Calculation of hard disk capacity. Difficulty [☆☆☆] View 2,012 Replies 14
Original Poster Posted 2007-05-08 23:04 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehe, this issue of the challenge series didn't appear on Monday on time, please forgive me (because of being busy with work and learning to debug equipment with a few expert-level people, I didn't have time).

Okay, enough chit-chat, let's get to the main topic now (calculation of hard disk capacity).

Now there is a hard disk, but we don't know how many partitions it has, the capacity of each partition, and the remaining space.
Suppose the space of this hard disk is: 300G, according to the division standard of the hard disk manufacturer, it is:
300*1000*1000*1000 in capacity.
The hard disk is divided into 4 partitions, among which
Drive letter Total capacity Remaining capacity
-------------------------------------------------------------------------------
C: 20*1024*1024*1024 1000*1024*1024
D: 40*1024*1024*1024 30*1024*1024*1024
E: 80*1024*1024*1024 60*1024*1024*1024
The remaining is drive F: We already know that 20*1024*1024*1024 of capacity has been used for drive F:.
-------------------------------------------------------------------------------
So now the question is: What is the actual capacity of drive F:?

Requirement: Save the above file as: Drivers.txt

Read this hard disk capacity file from the file and perform the calculation.

For the calculation of capacity, the rule of rounding is adopted. The basic unit is M. For the remaining capacity less than 1G, calculate the actual size (accurate to M). For the unit over 1G, it is G, but it is required to be accurate to three decimal places.

Program usage requirements: CMD (third-party tools can be used, can be combined with vbs, or can be completed alone with vbs)

For those who use third-party tools to complete the goal (points: 2 points)
For those who use CMD completely to complete (points: 5 points, full score)

Difficulty: Friends who extract the correct capacity: 1 point
Friends who use the division operation correctly: 1 point
Friends who use the accurate unit well: 1 point
Friends who get the correct capacity of drive F: 2 points.
Drivers.txt
-------------------------------------------------------------------------------
C: 20*1024*1024*1024 1000*1024*1024
D: 40*1024*1024*1024 30*1024*1024*1024
E: 80*1024*1024*1024 60*1024*1024*1024
F: ????? (???-20*1024*1024*1024)
-------------------------------------------------------------------------------
set "Volume_C=" ; accurate to bytes
set "Volume_D="
set "Volume_E="
set "Volume_F="
set "Real_Volume_C=" accurate to M or G
Dividend: dividend
Divisor: divisor
Quotient: quotient

Okay, let's start doing the problem now.

Requirement to output

C: %Volume_C%(%Real_Volume_C% ) Here M or G can only exist one.
d: %Volume_D%(%Real_Volume_D% )
e: %Volume_E%(%Real_Volume_E% )
f: %Volume_F%(%Real_Volume_F% )

Output:
For example: c: 1048576(1000M)
d: 32212254720(30G)
Use M as the unit for capacity less than 1G
Use G as the unit for capacity greater than 1G

[ Last edited by flyinspace on 2007-5-9 at 01:23 PM ]
知,不觉多。不知,乃求知
Floor 2 Posted 2007-05-09 00:13 ·  中国 广东 广州 白云区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
To obtain the space of all partitions in the system (in bytes), you can use the following code. As for precisely calculating extremely large numbers, it's too cumbersome, and it's left to the next person -_-
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "skip=1 delims=" %%i in ('fsutil fsinfo drives ^| more') do (
set a=%%i
for /f "skip=1 delims=" %%a in ('wmic LOGICALDISK where Caption^="!a:~-3,-1!" get size') do echo !a:~-3,-2! %%a
)
pause


[ Last edited by dikex on 2007-5-9 at 12:36 AM ]
正在潜水修练的批处理小白
Floor 3 Posted 2007-05-09 09:46 ·  中国 广东 广州 白云区 电信
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
It seems that the person above has misunderstood the LZ's meaning.
The question in 1F looks more like a calculation problem.
Rather than querying the hard disk.
Floor 4 Posted 2007-05-09 09:50 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
You need to indicate what file system it is, as it affects the accuracy of the final available space calculation.
Floor 5 Posted 2007-05-09 11:54 ·  中国 新疆 乌鲁木齐 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
Does this problem have anything to do with the file system? It's just pure multiplication and division of numbers. The difficulty lies in reading the text. It's relatively easier to read with P, but the value is too large to calculate. VBS can be used for calculation but has some difficulties in reading.
Floor 6 Posted 2007-05-09 12:23 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
To youxi01:
Mainly to query the capacity of the hard disk (apparently there are large number operations, but here, a series of methods can be used to bypass it.)
To everest79:
Brother, you're overthinking. It's just to test comprehensive ability (if you really don't take shortcuts, the content includes addition, subtraction, multiplication, division, and remainder operations.)
Then here, the issue of subroutine calls will be involved, because if a large number of four arithmetic operations are still put together, the length of the code is unimaginable : )
Those with beautiful code, flyinspace will additionally add 3 points here (the difficulty is: reasonable matching, no need to comment every sentence, but need to mark what this subroutine is for in front of the subroutine. Just wrote a small four arithmetic operation program in C++, which is just to call each subroutine and see if it is correct.)
ps: Now I'm increasingly feeling the importance of algorithms. Everyone can go to the Internet when writing this script, and it is recommended to find posts about large number operations on CSDN.
知,不觉多。不知,乃求知
Floor 7 Posted 2007-05-09 13:13 ·  中国 新疆 乌鲁木齐 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
I'll interject, omitting the reading of the text. Hehe
Just some things I don't quite understand the meaning of the LZ's title...



'C drive
uscb=20*1024*1024*1024-1000*1024*1024
uscm=uscb/1024/1024
uscg=uscm/1024
uscg=Round(uscg,3)

alcb=20*1024*1024*1024
alcm=alcb/1024/1024
alcg=alcm/1024
alcg=Round(alcg,3)

frcb=1000*1024*1024
frcm=frcb/1024/1024
frcg=frcm/1024
frcg=Round(frcg,3)

'D drive
usdb=40*1024*1024*1024-30*1024*1024*1024
usdm=usdb/1024/1024
usdg=usdm/1024
usdg=Round(usdg,3)

aldb=40*1024*1024*1024
aldm=aldb/1024/1024
aldg=aldm/1024
aldg=Round(aldg,3)

frdb=30*1024*1024*1024
frdm=frdb/1024/1024
frdg=frdm/1024
frdg=Round(frdg,3)

'E drive
useb=80*1024*1024*1024-60*1024*1024*1024
usem=useb/1024/1024
useg=usem/1024
useg=Round(useg,3)

aleb=80*1024*1024*1024
alem=aleb/1024/1024
aleg=alem/1024
aleg=Round(aleg,3)

freb=60*1024*1024*1024
frem=freb/1024/1024
freg=frem/1024
freg=Round(freg,3)


'F drive
usfb=20*1024*1024*1024
usfm=usfg/1024/1024
usfg=usfm/1024
usfg=Round(usfg,3)

alfb=300*1024*1024*1024-alcb-aldb-aleb
alfm=alfb/1024/1024
alfg=alfm/1024
alfg=Round(alfg,3)

frfb=alfb-usfb
frfm=frfg/1024/1024
frfg=frfm/1024
frfg=Round(frfg,3)




MsgBox "Total C drive size: " & alcg & "G" & " " & alcm & "M" & " " & alcb & "B" & Chr(10) & "Used space on C drive:" & uscg & "G" & " " & uscm & "M" & " " & uscb & "B" & Chr(10) & "Available space on C drive:" & frcg & "G" & " " & frcm & "M" & " " & frcb & "B" & Chr(10) & Chr(10) & "Total D drive :" & aldg & "G" & " " & aldm & "M" & " " & aldb & "B" & Chr(10) & "Used space on D drive:" & usdg & "G" & " " & usdm & "M" & " " & usdb & "B" & Chr(10) & "Available space on D drive:" & frdg & "G" & " " & frdm & "M" & " " & frdb & "B" & Chr(10) & Chr(10) & "Total E drive :" & aleg & "G" & " " & alem & "M" & " " &aleb& "B" & Chr(10) & "Used space on E drive:" & useg & "G" & " " & usem & "M" & " " & useb & "B" & Chr(10) & "Available space on E drive:" & freg & "G" & " " & frem & "M" & " " & freb & "B" & Chr(10) & Chr(10) & "Total F drive :" & alfg & "G" & " " & alfm & "M" & " " & alfb & "B" & Chr(10) & "Used space on F drive:" & usfg & "G" & " " & usfm & "M" & " " & usfb & "B" & Chr(10) & "Available space on F drive:" & frfg & "G" & " " & frfm & "M" & " " & frfb & "B", vbInformation, "$$$Made by Ye Qi$$$"


Floor 8 Posted 2007-05-09 13:15 ·  中国 新疆 乌鲁木齐 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
Uh... The available space on drive C seems to be miswritten
Floor 9 Posted 2007-05-09 23:50 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Then the LZ's meaning is: First read the number of disks, then read out the capacity and other information of the previous drive letters, and keep the last one to be calculated (that is, not allowed to read). Is this the meaning?
Floor 10 Posted 2007-05-10 16:51 ·  中国 山东 枣庄 滕州市 联通
初级用户
Credits 61
Posts 32
Joined 2007-05-09 17:09
19-year member
UID 87974
Gender Male
Status Offline
Learning
Floor 11 Posted 2007-05-10 17:19 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Originally posted by youxi01 at 2007-5-9 11:50 PM:
So the meaning of the original poster is:

First read the number of disks, then read the capacity and other information of the previous drive letters, and keep the last one to be calculated (that is, not allowed to read). Is that the meaning?

Hmm, yes. ..

All items have given conditions...

It can be calculated by four arithmetic operations. ..
知,不觉多。不知,乃求知
Floor 12 Posted 2009-01-10 18:11 ·  中国 广东 深圳 电信
初级用户
★★
Credits 185
Posts 88
Joined 2008-05-12 18:46
18-year member
UID 118511
Gender Male
Status Offline
Can you send one that can also be used under VISTA?
Floor 13 Posted 2009-01-11 23:04 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
@echo off&setlocal enabledelayedexpansion
::Temporary variables in for are _v1, _v2, _v(n)
::disk=Total hard disk capacity/1024
::_n=Number of partitions

::for1 Read correct string, if it is drive f:, remove symbols like??-() etc.
:: _d:Drive letter _zc:Total capacity string _yc:Used space string

::for2 Segment the string
:: _zt:Head part of total capacity string _zw:Tail _yt:Head part of used space string _yw:Tail

::for3 Calculate total space and free space of F:
:: Use temporary variables _v1, _v2, _v3

::for4 Process results in units of M/G
:: _zm:Total capacity _sm:Free space _ym:Used space

::for5 Process into bytes (B), megabytes M/G
:: _zb:Total bytes B _yb:Used bytes B _sb:Free bytes

::for6 Display output


echo off
set /a disk=300*1024*1024

::for1 Read correct numeric string, remove??() etc.
set _n=0
echo. Reading data&echo.************************************
for /f "tokens=1,2,3* delims= " %%i in ('findstr : drivers.txt') do (
set /a _n+=1&echo %%i %%j %%k
set _v1=%%i&set _v2=%%j&set _v3=%%k
if "!_v1!"=="F:" set _v2=!_v2:?=!&set _v3=!_v3:*-=!&set _v3=!_v3:^)=!&set _y!_n!=!_v3!
set _d!_n!=!_v1!&set _z!_n!=!_v2!&set _s!_n!=!_v3!
)
echo.************************************&echo.&echo.

::for2 Segment processing of numeric string
:: _zt:Head part of total capacity string _zw:Tail _yt:Head part of used space string _yw:Tail
for /l %%a in (1,1,!_n!) do (
set _v2=!_z%%a!&set _v3=!_s%%a!&set _v4=!_y%%a!
set _zt%%a=!_v2:~0,-5!
set _zw%%a=!_v2:~-4!
set _st%%a=!_v3:~0,-5!
set _sw%%a=!_v3:~-4!
set _yt%%a=!_v4:~0,-5!
set _yw%%a=!_v4:~-4!
)


::for3 Calculate total space and free space of F:
:: Use temporary variables _v1, _v2, _v3
set _v1=
for /l %%a in (1,1,!_n!) do (
set /a _zt%%a=!_zt%%a!&set /a _st%%a=!_st%%a!&set /a _yt%%a=!_yt%%a!
if "!_d%%a!"=="F:" (set /a _zt%%a=!disk!-!_v1!&set /a _st%%a=!_zt%%a!-!_yt%%a!&set _zw%%a=!_yw%%a!) else (set /a _yt%%a=!_zt%%a!-!_st%%a!&set _yw%%a=!_zw%%a!&set /a _v1=!_v1!+!_zt%%a!)
)


::for4 Calculate results in units of M, if greater than 1000M, use G as unit
:: _zm:Total capacity _sm:Free space _ym:Used space
for /l %%a in (1,1,!_n!) do (
set /a _zm%%a=!_zt%%a!/1024&set /a _sm%%a=!_st%%a!/1024&set /a _ym%%a=!_yt%%a!/1024
if !_zm%%a! leq 1000 (set _zm%%a=^(!_zm%%a!M^)) else (set /a _zm%%a=!_zm%%a!/1024 & set _zm%%a=^(!_zm%%a!G^))
if !_sm%%a! leq 1000 (set _sm%%a=^(!_sm%%a!M^)) else (set /a _sm%%a=!_sm%%a!/1024 & set _sm%%a=^(!_sm%%a!G^))
if !_ym%%a! leq 1000 (set _ym%%a=^(!_ym%%a!M^)) else (set /a _ym%%a=!_ym%%a!/1024 & set _ym%%a=^(!_ym%%a!G^))
)


::for5 Process into byte display (B)
:: _zb:Total B _yb:Used B _sb:Free bytes
for /l %%a in (1,1,!_n!) do (
rem Multiply %1 by %2 and put it in the variable pointed to by %3
call :sub !_zt%%a! !_zw%%a! _zb%%a
call :sub !_st%%a! !_sw%%a! _sb%%a
call :sub !_yt%%a! !_yw%%a! _yb%%a
)

::for6 Display output
echo.Hard disk data:
echo._______________________________________________________________________________
for /l %%a in (1,1,!_n!) do (echo.!_d%%a! Total capacity:!_zb%%a!!_zm%%a! Free:!_sb%%a!!_sm%%a! Used:!_yb%%a!!_ym%%a!)
echo._______________________________________________________________________________
pause
goto :eof

:sub
set _v1=%1&set _v2=%2&set _v3=%3
set /a _vt=!_v1:~0,-5!*!_v2!
set /a _vw=!_v1:~-5!*!_v2!
set _vx=!_vt!00000
set /a _vy=!_vx:~-9!+!_vw!
set _vx=!_vx:~0,-9!
set _v4=!_vy:~0,-9!
if not "!_va!"=="" set /a _vx=!_vx!+!_v4!
set !_v3!=!_vx!!_vy!

goto :eof

[ Last edited by netbenton on 2009-1-11 at 23:07 ]
精简
=> 个人网志
Floor 14 Posted 2009-01-12 09:40 ·  中国 北京 联通
银牌会员
★★★
Credits 1,287
Posts 634
Joined 2007-05-02 15:06
19-year member
UID 87277
Gender Male
From cmd.exe
Status Offline
DISKPART> list disk

The definition of the external command has not been... That's it...
Floor 15 Posted 2009-01-12 14:46 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
Puzzled:
Maybe you still don't understand the LZ's meaning!
What external commands need to be defined?
精简
=> 个人网志
Forum Jump: