wmic, overall disk capacity, convert bytes to gb

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

wmic, overall disk capacity, convert bytes to gb

#1 Post by BoQsc » 24 Oct 2018 13:35

Here is a problem:
When a single USB is connected, the device capacity in gigabytes are calculated correctly.
But, when I tried to connect one more USB flash drive, the newly connected flash drive's capacity is displayed incorrectly. (way bigger)
Is this weird only for me?

Code: Select all

@echo off
setlocal enabledelayedexpansion


:: https://superuser.com/questions/1192107/wmic-output-property-value-without-property-name/1192171#1192171
for /f "tokens=* skip=1" %%a in ('"wmic diskdrive where(InterfaceType="USB") get Size | findstr ".""') do (

set "size="
set "bytes="
set "kb="
set "mb="
set "gb="

set "size=%%a"
set "bytes=!size:~0,-4!"
set /a kb = bytes/1024
set /a mb = kb*100/1024
set /a gb = mb/1024
set /a gb = !gb:~0,-1! 
echo !gb!GB
:: Output 
:: 19GB (actually 7GB USB)
:: 14GB (this one correct)

)





pause

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: wmic, overall disk capacity, convert bytes to gb

#2 Post by penpen » 26 Oct 2018 03:43

Are the sizes of your usb devices correctly displayed by wmic:

Code: Select all

wmic diskdrive where(InterfaceType="USB") get Size
If the sizes are OK, then it would help, if your wmic-statement output.


penpen

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: wmic, overall disk capacity, convert bytes to gb

#3 Post by BoQsc » 05 Jan 2023 11:11

It's probably a division accuracy. Numbers are limited to 32-bits of precision.

Post Reply