user3164140
user3164140

Reputation: 651

Disk size in Windows for particular drive

I want to know disk available size for particular drive say D, in GB. I am using below wmic command,

wmic logicaldisk get size,freespace,caption

but it is giving info about each drive, I want for particular drive only and that even in GB.

Upvotes: 2

Views: 3095

Answers (4)

Ofir Luzon
Ofir Luzon

Reputation: 10947

I have a batch file that verifies HDD, Mem, OS and CPU arch, I ripped it a bit to fit your needs. Just set the wanted drive followed by :. To convert the bytes to GB, I first trim in 1000 until the value can fit in 32bits and only then I divide in 1024. (cmd can only handle 32 bit integers).

@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

Upvotes: 0

foxidrive
foxidrive

Reputation: 41297

This dual routine shows the free space on all drives. You can use the main batch file to find the space free on a single drive.

@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

Upvotes: 1

npocmaka
npocmaka

Reputation: 57332

for /f "delims=" %%a in ('wmic logicaldisk where ^"DeviceID^=^'D:^'^" get size^,freespace^,caption /format:Wmiclivalueformat.xsl^|find "="') do @set %%a

As the numbers could be pretty big and overflows the integers you can get get the free space in GB by getting a substring (be grateful that this requires only division by powers of 10 - though it's not pretty accurate as the real division should be by 1024):

set freespaceinGB=%freespace:~0,-10%
set freespaceinGB_MOD=%freespace:~-10,-8%
echo %freespaceinGB%,%freespaceinGB_MOD%

Upvotes: 1

AJ101
AJ101

Reputation: 69

If you know the VB script then Check this Microsoft link this might resolve your issue. http://technet.microsoft.com/en-us/library/ee198873.aspx

You just need to copy that code in notepad and save it as .vbs format and execute the file from commandline. command will be like this. cscript filename.vbs

Upvotes: 0

Related Questions