Post by H. DrussUsing Win32_LogicalDisk to get the size of the fixed drives.
How can I convert this to Megabytes or Gigabytes?
objItem.Size/1024 does not seem to work.
The data type of the size property of this class is _uint64_. When
querying for this property value in VBScript, WMI returns a string
value. To avoid confusion you should use the *CDbl* method for
converting this value into a (decimal) number. If you execute this line
in the interpreter:
WScript.Echo "Size: " & TypeName(wmiDisk.Size)
you will receive this output:
| Size: String
If the value has been converted into a number, you can calculate the
number of kilobytes: CDbl(wmiDisk.Size) / number. For kilobytes it is
advisable to use the power of two, one kilobyte corresponds to the tenth
power of 2 or 1024 Bytes. In VBScript you can use the caret character to
compute powers: 2^10.
Take a look at an example code:
WScript.Echo "Size: " & CDbl(wmiDisk.Size) / 2^10 & " Kilobytes"
One Megabyte are 1,024 bytes multiplied by itself, this results is the
20th power of base 2 in bytes: 2^20. Then to gigabyte counts 1024
multiplied 3 times with itself: 2^30 Bytes. You must divide the value in
bytes by this number to get the amount of gigabytes. Now the value can
be calculated:
WScript.Echo "Size: " & CDbl(wmiDisk.Size) / 2^20 & " Megabytes"
WScript.Echo "Size: " & CDbl(wmiDisk.Size) / 2^30 & " Gigabytes"
However, it should not be missed the fact that the hard disk
manufacturers count on the decimal base, then there is 1 Kilobyte 1,000
Bytes. For such values this results in:
WScript.Echo "Size: " & CDbl(wmiDisk.Size) / 10^9 & " Gigabytes"
*Remark*: CDbl converts a numeric expression into a flowing-point
(decimal) number, if possible.
--
ЯR