Discussion:
Convert disk size to megabytes
(too old to reply)
H. Druss
2009-03-22 11:05:50 UTC
Permalink
Hi
Using 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.
I'm new at this.
Thanks
Harold
Pegasus [MVP]
2009-03-22 11:26:44 UTC
Permalink
Post by H. Druss
Hi
Using 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.
I'm new at this.
Thanks
Harold
Let's have a look at your code.
Richard Mueller [MVP]
2009-03-22 21:54:42 UTC
Permalink
Post by H. Druss
Hi
Using 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.
I'm new at this.
Thanks
Harold
The Size property exposed by the Win32_DiskPartion class of WMI, the Size
property of the Win32_LogicalDisk class, the Size property of the
Win32_DiskDrive class, and the FreeSpace, AvailableSpace, and TotalSize
properties of the Drive Objects exposed by the FileSystemObject are all in
bytes. To convert to megabytes divide by 1,048,576 (1024^2). To convert to
gigabytes divide by 1,073,741,824 (1024^3).
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Ruediger Roesler
2009-03-25 16:47:25 UTC
Permalink
Post by H. Druss
Using 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
H. Druss
2009-03-26 10:08:14 UTC
Permalink
Post by Ruediger Roesler
Post by H. Druss
Using 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
WScript.Echo "Size: " & TypeName(wmiDisk.Size)
| 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.
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
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
WScript.Echo "Size: " & CDbl(wmiDisk.Size) / 10^9 & " Gigabytes"
*Remark*: CDbl converts a numeric expression into a flowing-point
(decimal) number, if possible.
--
?R
Hi Ruediger
Thanks for the reply. After Richard's reply I wrote this function. I'll
modify it with your information.
==================================================================
Private Function ConvertNumber(n As Variant) As String
Dim s As String
s = Trim$(Str(n))
Select Case Len(s)
Case Is < 4
ConvertNumber = s & " Bytes"
Case Is < 7
ConvertNumber = CStr(CCur(s) / 1024) & " KB"
Case Is < 10
ConvertNumber = CStr(Format$(CCur(s) / (1024 ^ 2), "#,###.#")) & "
MB"
Case Else
ConvertNumber = CStr(Format$(CCur(s) / (1024 ^ 3), "#,###.#")) & "
GB"
End Select
End Function
===================================================================
Ruediger Roesler
2009-03-26 20:29:14 UTC
Permalink
Post by H. Druss
Thanks for the reply. After Richard's reply I wrote this function.
I'll modify it with your information.
This must probably have been a misunderstanding. I have assumed, it
would be here a news group about script languages. However, in the
essentials my statements also apply to Visual BASIC.
--
ЯR
Pegasus [MVP]
2009-03-26 21:15:16 UTC
Permalink
Post by Ruediger Roesler
Post by H. Druss
Thanks for the reply. After Richard's reply I wrote this function.
I'll modify it with your information.
This must probably have been a misunderstanding. I have assumed, it
would be here a news group about script languages. However, in the
essentials my statements also apply to Visual BASIC.
--
?R
No, it wasn't a misunderstanding. The OP never said what language he was
using and he did not post his code until four days after his initial post.
This creates plenty of potential for running off in the wrong direction.
H. Druss
2009-03-27 09:46:21 UTC
Permalink
Post by Pegasus [MVP]
Post by Ruediger Roesler
Post by H. Druss
Thanks for the reply. After Richard's reply I wrote this function.
I'll modify it with your information.
This must probably have been a misunderstanding. I have assumed, it
would be here a news group about script languages. However, in the
essentials my statements also apply to Visual BASIC.
--
?R
No, it wasn't a misunderstanding. The OP never said what language he was
using and he did not post his code until four days after his initial post.
This creates plenty of potential for running off in the wrong direction.
Hi
I"m sorry if I caused any confusion, however the replies solved my problem.
I'm using vbscript to gather the information (disk information) and vb6 as a
front end.
I guess my question was how to format the size and free space.
I did not realize the value was a string.
Again, I aploigize for any confusion I might have caused. As I said, I'm new
at this.
Part of my code below.
Thanks
Harold

==================================================================
' fill a grid with the return values
Dim objItem
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk",
"WQL", _
wbemFlagReturnImmediately +
wbemFlagForwardOnly)
For Each objItem In colItems
fg.Cell(flexcpText, iRow, iCol) = objItem.Name
iCol = iCol + 1

fg.Cell(flexcpText, iRow, iCol) = GetDriveType(objItem.drivetype)
iCol = iCol + 1

If Not IsNull(objItem.Size) Then fg.Cell(flexcpText, iRow, iCol) =
ConvertNumber(objItem.Size)
iCol = iCol + 1

If Not IsNull(objItem.freespace) Then fg.Cell(flexcpText, iRow, iCol) =
ConvertNumber(objItem.freespace)

iRow = iRow + 1 ' this must be after the last item in the list
iCol = 0 ' this must be after the last item in the list
Next
========================================================================
Loading...