List Internal Disks

The following lists only Internal Fixed Disks (uses wmic.exe)

Disk 0 466GiB "Samsung SSD 850 EVO 500GB" (500105MB, 500105249280 Bytes)
Disk 1 1863GiB "ST2000DX001-1CM164" (2000396MB, 2000396321280 Bytes)
Disk 2 1863GiB "TOSHIBA DT01ACA200" (2000396MB, 2000396321280 Bytes)
Disk 3 932GiB "SanDisk SDSSDH31000G" (1000202MB, 1000202273280 Bytes)


@echo off

cls

color 1f

set C=0


:LOOP

call :check_fixed %C%

if "%FIXED%"=="1" call :drivesize %C%

if "%FIXED%"=="1" echo Disk %C% %sizeGiB%GiB "%Caption%" (%sizeMB%MB, %sizeB% Bytes)

set /a C=%C%+1

if %C% LSS 27 goto :LOOP

goto :EOF


::-----------------------------------------


:check_fixed

set FIXED=0

REM use wmic for USB and Removable media detection

wmic DISKDRIVE where deviceid='\\\\.\\PHYSICALDRIVE%1' get Index, MediaType, InterfaceType 2>nul | find /i "Fixed" > nul && set FIXED=1

goto :eof


::-----------------------------------------

:drivesize

if "%~1"=="" goto :eof

set "sizeMB=" & set "sizeB=" & set "sizeGiB=" & set "Caption="


for /f "usebackq skip=1 tokens=1,2" %%X in (`

    wmic diskdrive where "index='%1'" get size

  `) do if not defined sizeB (set "sizeB=%%X")


for /f "usebackq skip=1 tokens=*" %%X in (`

    wmic diskdrive where "index='%1'" get Caption

  `) do if not defined Caption (set "Caption=%%X")


set Caption=%Caption:~0,-3%


set/a sizeMB = %sizeB:~0,-6%

call :mb2gib sizeMB sizeGiB

exit /b


:mb2gib

@rem double 1000/1024 mb->mib correction

set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128

@rem 1/1024 mib->gib conversion

set/a %2 = (%2 + 512) / 1024

Exit /b


@echo off

setlocal enabledelayedexpansion



set DISKNO=0

call :drivesize %DISKNO%

echo XXXX Disk%DISKNO% %sizeGiB%GiB %sizeMB%MB "%Caption%"



set DISKNO=1

call :drivesize %DISKNO%

echo XXXX Disk%DISKNO% %sizeGiB%GiB %sizeMB%MB "%Caption%"

set DISKNO=2

call :drivesize %DISKNO%

echo XXXX Disk%DISKNO% %sizeGiB%GiB %sizeMB%MB "%Caption%"

set DISKNO=3

call :drivesize %DISKNO%

echo XXXX Disk%DISKNO% %sizeGiB%GiB %sizeMB%MB "%Caption%"

set DISKNO=4

call :drivesize %DISKNO%

echo XXXX Disk%DISKNO% %sizeGiB%GiB %sizeMB%MB "%Caption%"


REM List all drives


for /f "usebackq skip=1" %%X in (`

    wmic DISKDRIVE where "mediatype='Fixed hard disk media'" 2^>nul get index

  `) do (

REM %%X does something weird at end of loop, make sure it is valid number

set dno=

set /A dno=%%X 2> nul

if not @!dno!@==@@ call :drivesize %dno%

if not @!dno!@==@@ echo Disk%%X !sizeGiB!GiB !sizeMB!MB "%Caption%"

)


goto :EOF


::-----------------------------------------

:drivesize

if "%~1"=="" goto :eof

set "sizeMB=" & set "sizeB=" & set "sizeGiB=" & set "Caption="


for /f "usebackq skip=1 tokens=1,2" %%X in (`

    wmic diskdrive where "index='%1'" get size

  `) do if not defined sizeB (set "sizeB=%%X")


for /f "usebackq skip=1 tokens=*" %%X in (`

    wmic diskdrive where "index='%1'" get Caption

  `) do if not defined Caption (set "Caption=%%X")


set Caption=%Caption:~0,-3%


set/a sizeMB = %sizeB:~0,-6%

call :mb2gib sizeMB sizeGiB

exit /b


:mb2gib

@rem double 1000/1024 mb->mib correction

set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128

@rem 1/1024 mib->gib conversion

set/a %2 = (%2 + 512) / 1024

Exit /b


This code lists disk size and caption in GiB for first 5 disks in system - only if fixed disk (not removable). Note special backquotes used in drivesize routine - lines 3 and 4 !

@echo off

setlocal enabledelayedexpansion

set DISKNO=0

call :drivesize %DISKNO%

if not "%sizeB%"=="" echo Disk %DISKNO% %sizeGiB%GiB  %Caption%

set DISKNO=1

call :drivesize %DISKNO%

if not "%sizeB%"=="" echo Disk %DISKNO% %sizeGiB%GiB  %Caption%

set DISKNO=2

call :drivesize %DISKNO%

if not "%sizeB%"=="" echo Disk %DISKNO% %sizeGiB%GiB  %Caption%

set DISKNO=3

call :drivesize %DISKNO%

if not "%sizeB%"=="" echo Disk %DISKNO% %sizeGiB%GiB  %Caption%

set DISKNO=4

call :drivesize %DISKNO%

if not "%sizeB%"=="" echo Disk %DISKNO% %sizeGiB%GiB  %Caption%

goto :EOF


::-----------------------------------------


:drivesize

if "%~1"=="" goto :eof

set "sizeMB=" & set "sizeB=" & set "sizeGiB=" & set "Caption="

for /f "usebackq skip=1 tokens=1,2" %%X in (`wmic diskdrive where "index='%1' AND mediatype='Fixed hard disk media'" get size 2^> nul`) do if not defined sizeB (set "sizeB=%%X" > nul)

for /f "usebackq skip=1 tokens=*" %%X in   (`wmic diskdrive where "index='%1' AND mediatype='Fixed hard disk media'" get Caption 2^> nul`) do if not defined Caption (set "Caption=%%X" > nul)

if "%sizeB%"==""  exit /b

set Caption=%Caption:~0,-3% > nul

set /a sizeMB = %sizeB:~0,-6% > nul

call :mb2gib sizeMB sizeGiB

if "%sizeGiB:~2,1%"=="" set "sizeGiB= %sizeGiB%"

if "%sizeGiB:~3,1%"=="" set "sizeGiB= %sizeGiB%"

if "%sizeGiB:~4,1%"=="" set "sizeGiB= %sizeGiB%"

if "%sizeGiB:~5,1%"=="" set "sizeGiB= %sizeGiB%"

if "%sizeGiB:~6,1%"=="" set "sizeGiB= %sizeGiB%"

if "%Caption:~-1,1%"==" " set Caption=%Caption:~0,-1%

exit /b


:mb2gib

@rem double 1000/1024 mb->mib correction

set /a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128 > nul

@rem 1/1024 mib->gib conversion

set /a %2 = (%2 + 512) / 1024 > nul

exit /b