我正在尝试获取磁盘空间,如果它是MB GB或具有2个小数部分的TB。 它应该给出带小数的输出
这是我的代码:
WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:"
答案 0 :(得分:0)
这是我对另一个问题的回答稍加修改的脚本,它构成了一个纯粹的batch-file解决方案:Get size of a directory in 'MB' using batch file。
基本上,所有的计算都是用右边两个零填充的数字来完成的,这与乘以一百相同,小数分隔符插在右边的第二个数字旁边,仅用于显示。获取可用空间的驱动器需要作为命令行参数给出,例如C:
。
以下是代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set /A DIVISOR=1024 & rem (1024 Bytes = 1 KiB, 1024 KiB = 1 MiB,...)
set "ROUNDUP=" & rem (set to non-empty value to round up results)
set /A DECIMALS=2 & rem (define number of decimal places here)
set "PAD=" & for /L %%N in (1,1,%DECIMALS%) do call set "PAD=%%PAD%%0"
rem Get free space size of drive given as command line argument:
for /F "tokens=2 delims== " %%B in ('
wmic LogicalDisk WHERE 'DeviceID^="%~1"' GET FreeSpace /VALUE
') do for /F %%A in ("%%B") do set "BYTES=%%A"
if not defined BYTES set "BYTES=0"
rem Display result in Bytes and divide it to get KiB, MiB, etc.:
call :DIVIDE %BYTES%%PAD% 1 RESULT
call :DISPLAY %RESULT% %DECIMALS% STRING
echo( Bytes: %STRING%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
call :DISPLAY %RESULT% %DECIMALS% STRING
echo(KiBytes: %STRING%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
call :DISPLAY %RESULT% %DECIMALS% STRING
echo(MiBytes: %STRING%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
call :DISPLAY %RESULT% %DECIMALS% STRING
echo(GiBytes: %STRING%
call :DIVIDE %RESULT% %DIVISOR% RESULT REST
if defined ROUNDUP if 0%REST% GTR 0 set /A RESULT+=1
call :DISPLAY %RESULT% %DECIMALS% STRING
echo(TiBytes: %STRING%
endlocal
exit /B
:DIVIDE val_dividend val_divisor [ref_result] [ref_remainder]
rem Divide a huge number exceeding the 32-bit limitation
rem by a 32-bit number in the range from 1 to 1000000000;
rem the result might also exceed the 32-bit limitation.
setlocal EnableDelayedExpansion
set "DIVIDEND=%~1"
set "DIVISOR=%~2"
set "QUOTIENT=%~3"
set "REMAINDER=%~4"
rem Check whether dividend and divisor are given:
if not defined DIVIDEND (
>&2 echo(Too few arguments, dividend missing^^!
exit /B 2
) else if not defined DIVISOR (
>&2 echo(Too few arguments, divisor missing^^!
exit /B 2
)
rem Check whether dividend is purely numeric:
for /F "tokens=* delims=0123456789" %%N in ("!DIVIDEND!") do (
if not "%%N"=="" (
>&2 echo(Dividend must be numeric^^!
exit /B 2
)
)
rem Convert divisor to numeric value without leading zeros:
for /F "tokens=* delims=0" %%N in ("%DIVISOR%") do set "DIVISOR=%%N"
set /A DIVISOR+=0
rem Check divisor against its range:
if %DIVISOR% LEQ 0 (
>&2 echo(Divisor value must be positive^^!
exit /B 1
) else if %DIVISOR% GTR 1000000000 (
>&2 echo(Divisor value exceeds its limit^^!
exit /B 1
)
set "COLL=" & set "NEXT=" & set /A INDEX=0
rem Do a division digit by digit as one would do it on paper:
:LOOP
if "!DIVIDEND:~%INDEX%,1!"=="" goto :CONT
set "NEXT=!NEXT!!DIVIDEND:~%INDEX%,1!"
rem Remove trailing zeros as such denote octal numbers:
for /F "tokens=* delims=0" %%N in ("!NEXT!") do set "NEXT=%%N"
set /A NEXT+=0
set /A PART=NEXT/DIVISOR
set "COLL=!COLL!!PART!"
set /A NEXT-=PART*DIVISOR
set /A INDEX+=1
goto :LOOP
:CONT
rem Remove trailing zeros as such denote octal numbers:
for /F "tokens=* delims=0" %%N in ("%COLL%") do set "COLL=%%N"
if not defined COLL set "COLL=0"
rem Set return variables or display result if none are given:
if defined QUOTIENT (
if defined REMAINDER (
endlocal
set "%REMAINDER%=%NEXT%"
) else (
endlocal
)
set "%QUOTIENT%=%COLL%"
) else (
endlocal
echo(%COLL%
)
exit /B
:DISPLAY val_number val_decimals [ref_result]
rem Prepare fractional number for being displayed.
setlocal EnableDelayedExpansion
set "NUM=%~1"
set "DEC=%~2"
set "SEP=."
set "RET=%~3"
set "PAD=" & for /L %%N in (1,1,%DEC%) do set "PAD=!PAD!0"
set "NUM=%PAD%%NUM%"
set "NUM=!NUM:~,-%DEC%!%SEP%!NUM:~-%DEC%!"
for /F "tokens=* delims=0" %%N in ("%NUM%") do set "NUM=%%N"
if "%NUM:~,1%"=="%SEP%" set "NUM=0%NUM%"
if defined RET (
endlocal
set "%RET%=%NUM%"
) else (
endlocal
echo(%NUM%
)
endlocal
exit /B
以下是示例输出(假设脚本保存为drive-size.bat
):
C:\TEMP>drive-size.bat C: Bytes: 82752724992.00 KiBytes: 80813208.00 MiBytes: 78919.14 GiBytes: 77.06 TiBytes: 0.07