我想知道特定驱动器的磁盘可用大小,例如D,以GB为单位。 我正在使用wmic命令,
wmic logicaldisk get size,freespace,caption
但是它提供了有关每个驱动器的信息,我只想要特定的驱动器,即使是GB。
答案 0 :(得分:1)
for /f "delims=" %%a in ('wmic logicaldisk where ^"DeviceID^=^'D:^'^" get size^,freespace^,caption /format:Wmiclivalueformat.xsl^|find "="') do @set %%a
由于数字可能非常大并且溢出整数,你可以通过获得一个子字符串来获得GB中的可用空间(请记住,这只需要除以10的幂 - 尽管它不是很准确,因为真正的除法应该由1024):
set freespaceinGB=%freespace:~0,-10%
set freespaceinGB_MOD=%freespace:~-10,-8%
echo %freespaceinGB%,%freespaceinGB_MOD%
答案 1 :(得分:1)
此双重例程显示所有驱动器上的可用空间。您可以使用主批处理文件在单个驱动器上找到空闲空间。
@echo off
for %%A in (A B 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:\ call :bytesfree.bat %%A
pause
goto :EOF
:: Below is the batch file that returns the freespace from the letter in `%1`
@echo off
:bytesfree.bat - Salmon Trout
for /f "tokens=1-3 delims= " %%A in ('dir %1: ^| find "bytes free"') do set bytesfree="%%C"
echo Wscript.echo Formatnumber(eval(WScript.Arguments(0)/1073742268), 2, True,, False) > "%temp%\Calculate.vbs"
for /f "delims=" %%A in ('cscript //nologo "%temp%\Calculate.vbs" %bytesfree%') do set GBfree=%%A & del "%temp%\Calculate.vbs"
set "GBFree= %GBFRee%"
set GBFree=%GBFRee:~-12%
echo %1 %GBfree% GB free
goto :EOF
答案 2 :(得分:0)
如果你知道VB脚本,那么检查这个Microsoft链接可能会解决你的问题。 http://technet.microsoft.com/en-us/library/ee198873.aspx
您只需要在记事本中复制该代码并将其保存为.vbs格式并从命令行执行该文件。命令会是这样的。 cscript filename.vbs
答案 3 :(得分:0)
我有一个批处理文件,用于验证硬盘,内存,操作系统和CPU拱门,我将其翻录以满足您的需求。只需设置所需的驱动器,然后按:
。
要将字节转换为GB,我首先修剪1000,直到该值适合32位,然后除以1024.(cmd只能处理32位整数)。
@echo off setlocal EnableDelayedExpansion
rem UNIT is used to convert from Bytes to GB
rem 0 - B, 1 - KB, 2 - MB, 3 - GB
SET DRIVE="C:"
REM check all local drives for minimum space requirement
:Local_Drives_space_check
for /F "skip=1 tokens=1,2" %%a in ('wmic LogicalDisk Where DeviceID^=!DRIVE! Get FreeSpace') do (
if not "%%b"=="" (
set /A UNIT=0
set _tmp=%%a
call :TRIM !_tmp! space
echo drive !_drive:~0,1! has !space!GB free space
)
)
)
goto :end
REM :TRIM should get value to trim and a var name to store answer
REM examle: call :TRIM 536870912000 space
REM answer will be 512GB even though correct answer is 500GB
REM cmd.exe cannot handle numbers larger then 32bit
:TRIM
set _tmp=%1
set %2=
set /A %2=!_tmp! >nul 2>nul
if not defined %2 (
set _tmp=!_tmp:~0,-3!
set /A UNIT=!UNIT!+1
call :TRIM !_tmp! %2
goto :eof
)
:GB
if !UNIT! LSS 3 (
set /A %2=!%2!/1024
set /A UNIT=!UNIT!+1
)
if !UNIT! LSS 3 goto :GB
goto :eof
:end