Sunday, June 11, 2017

Windows command line to get physical drive size in GB


I want to display all the attached diskdrive model, name, index and size in command line
I am using the below simple code


    wmic diskdrive GET index,caption,name,size /format:table

Output


Caption                   Index  Name                Size
SAMSUNG SSD PM830 2.5" 7 0 \\.\PHYSICALDRIVE0 256052966400
Seagate backup plus 1 \\.\PHYSICALDRIVE1 536870912000

I want the same output as the above except that i would like the size to be displayed in GB


I think there is some way to do that using for loops and temp vb files.


I am specifically looking for windows cmd code. Can anybody help?


I am able to do the size conversion and display it on cmd


    for /f "tokens=1,2" %a in ('WMIC DISKDRIVE GET Name^,Size ^|FINDSTR /I /V "Name"') do @echo wsh.echo "%a" ^& " size=" ^& FormatNumber^(cdbl^(%b^)/1024/1024/1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%b"=="" @echo( & @cscript //nologo %temp%\tmp.vbs & del %temp%\tmp.vbs

Output


\\.\PHYSICALDRIVE0 Size=238.47 (GB)

But i am still not able to display the model name correctly.. I am just not able to get my for loop to run as intended (in my case the model name has spaces in between The below code works to just get the models printed.


    for /f "tokens=1-5 skip=1" %a in ('wmic diskdrive GET model') do @echo. %a %b %c %d %e

Output


SAMSUNG SSD PM830 2.5" 7

Can someone help me in combining both the codes such that it displays like


SAMSUNG SSD PM830 2.5" 7  \\.\PHYSICALDRIVE0  Size=238.47 (GB)

Answer



Calculations in cmd with set /A are limited to signed 32bit numbers, so you need vbscript or PowerShell to do the math.
Instead of using batch, wmic and vbscript I have here a hybrid batch/vbscript file which eliminates the need to create temporary files. A drawback is an echoed rem & which is required to have both script versions accept the file.


Save this as a .cmd/.bat file


rem^ &@cls&cscript //nologo //e:vbscript "%~f0"&Pause&exit /b
On Error Resume Next
sComputer = "."
Set oWMI = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set cItems = oWMI.ExecQuery("Select * from Win32_DiskDrive",,48)
sOut = ""
For Each oItem in cItems
sOut = sOut & JL(26,oItem.Model) & JL(20,oItem.Name) & "Size=" & _
JR(8,FormatNumber(cdbl(oItem.Size)/1024/1024/1024, 2)) & " (GiB)" &vbCRLF
Next
Set cItems = Nothing
wscript.echo sOut
Function JL(Places, Value) ' Justify Left within Places
If len (Value) < Places then
JL=Left(Value & Space(Places), Places)
Else
JL=Value
End If
end Function
Function JR(Places, Value) ' Justify Right within Places
If Len (Value) < Places then
JR = Right(Space(Places) & Value, Places)
Else
JR=Value
End If
end Function

Output on my system (german locale)


Hitachi HDS722020ALA330   \\.\PHYSICALDRIVE2  Size=1.863,01 (GiB)
VB0250EAVER \\.\PHYSICALDRIVE0 Size= 232,88 (GiB)
ST2000DL003-9VT166 \\.\PHYSICALDRIVE1 Size=1.863,01 (GiB)
Drücken Sie eine beliebige Taste . . .

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...