我试图通过wmic获取计算机中的物理驱动器信息,以获取类似内容:
Drive C:
500 GB Total
100 GB Free
20% Free
Drive D:
500 GB Total
100 GB Free
20% Free
到目前为止我的代码是:
for /f "usebackq tokens=*" %%a in (`wmic logicaldisk where "drivetype=3" get caption`) do (
echo "Drive=%%a"
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where DeviceID='%%a'"
get FreeSpace /format:value`) do set FreeSpace=%%x
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='%%a'" get Size /format:value`) do set Size=%%x
echo FreeMB=%FreeSpace%
echo SizeMB=%Size%
set /a Percentage=100 * %FreeSpace% / %Size%
echo %%a is %Percentage% %% free
)
我的输出是:
"Drive=Caption
No Instance(s) Available.
No Instance(s) Available.
FreeMB=9193357312
SizeMB=80024170496
Invalid number. Numbers are limited to 32-bits of precision.
is % free
"Drive=C:
No Instance(s) Available.
No Instance(s) Available.
FreeMB=9193357312
SizeMB=80024170496
Invalid number. Numbers are limited to 32-bits of precision.
is % free
"Drive=E:
No Instance(s) Available.
No Instance(s) Available.
FreeMB=9193357312
SizeMB=80024170496
Invalid number. Numbers are limited to 32-bits of precision.
is % free
"Drive=
No Instance(s) Available.
No Instance(s) Available.
FreeMB=9193357312
SizeMB=80024170496
Invalid number. Numbers are limited to 32-bits of precision.
is % free
我到了许多甚至没有驱动器的线路,我无法格式化值,因此我可以计算百分比,或以正常格式显示值
答案 0 :(得分:3)
这可能对您有用:代码将添加到批处理文件的底部,并使用
call :hdd-info
在您的代码中显示数据。
goto :eof
:code by aGerman - display drive stats and bar graph (REMmed out)
:hdd-info
@echo off &setlocal
set "GB=1073741824"
for /f "skip=1 delims=" %%i in ('wmic logicaldisk get DeviceID^,FreeSpace^,Size') do (
for /f "tokens=1-3" %%j in ("%%i") do call :output %%j %%k %%l
)
goto :eof
:output
if "%3"=="" (
rem echo Unable to discover the drive properties.
goto :eof
)
for /f "tokens=1-4" %%i in (
'mshta vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber(%2/%GB%, 2) & "" "" & Round((%3-%2)*50/%3)):Close"^)'
) do (
set "size= %%i"
set "used= %%j"
set "free= %%k"
set /a "nUsed=%%l, nFree=50-%%l"
)
echo(
echo %1
echo Size: %size:~-10% GB
echo Used: %used:~-10% GB
echo Free: %free:~-10% GB
:: echo(
:: for /l %%i in (1 1 %nUsed%) do <nul set /p "=▒"
:: for /l %%i in (1 1 %nFree%) do <nul set /p "=█"
:: echo(&echo(&echo(
goto :eof
答案 1 :(得分:0)
正如错误消息告诉你的那样,set
仅限于32位整数。如果您的精确度为MB
,则可以缩短这样的数字:
set SizeMB=%size:~0,-6%
(除最后6个字符外,它采用字符串)
在数学意义上可能不太正确,但应该足够好。
当然,您必须以同样的方式缩短%freeMB%
。
答案 2 :(得分:0)
wmic LogicalDisk where "DeviceID='C:' " Get Size