Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Convert disk size to megabytes

Reply
 
Old 03-22-2009   #1 (permalink)
H. Druss


 
 

Convert disk size to megabytes

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



My System SpecsSystem Spec
Old 03-22-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Convert disk size to megabytes


"H. Druss" <hdruss@xxxxxx> wrote in message
news:%23iaPq5tqJHA.724@xxxxxx
Quote:

> 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.


My System SpecsSystem Spec
Old 03-22-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: Convert disk size to megabytes


"H. Druss" <hdruss@xxxxxx> wrote in message
news:%23iaPq5tqJHA.724@xxxxxx
Quote:

> 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
--


My System SpecsSystem Spec
Old 03-25-2009   #4 (permalink)
Ruediger Roesler


 
 

Re: Convert disk size to megabytes

H. Druss <hdruss@xxxxxx> typed:
Quote:

> 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

My System SpecsSystem Spec
Old 03-26-2009   #5 (permalink)
H. Druss


 
 

Re: Convert disk size to megabytes


"Ruediger Roesler" <administrator@xxxxxx> wrote in message
news:49ca6078$1$31347$9b4e6d93@xxxxxx-online.net...
Quote:

> H. Druss <hdruss@xxxxxx> typed:
>
Quote:

>> 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
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
===================================================================


My System SpecsSystem Spec
Old 03-26-2009   #6 (permalink)
Ruediger Roesler


 
 

Re: Convert disk size to megabytes

H. Druss <hdruss@xxxxxx> typed:
Quote:

> 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

My System SpecsSystem Spec
Old 03-26-2009   #7 (permalink)
Pegasus [MVP]


 
 

Re: Convert disk size to megabytes


"Ruediger Roesler" <administrator@xxxxxx> wrote in message
news:49cbeb1a$0$32674$9b4e6d93@xxxxxx-online.net...
Quote:

> H. Druss <hdruss@xxxxxx> typed:
>
Quote:

>> 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.


My System SpecsSystem Spec
Old 03-27-2009   #8 (permalink)
H. Druss


 
 

Re: Convert disk size to megabytes


"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:edU31flrJHA.2368@xxxxxx
Quote:

>
> "Ruediger Roesler" <administrator@xxxxxx> wrote in message
> news:49cbeb1a$0$32674$9b4e6d93@xxxxxx-online.net...
Quote:

>> H. Druss <hdruss@xxxxxx> typed:
>>
Quote:

>>> 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
========================================================================





My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Estimate time to convert virtual disk to fixed size? Virtual PC
convert to dynamic disk Vista installation & setup
Convert USB disk to dynamic Vista General
Cannot convert to dyamic disk. Vista hardware & devices
Convert to Basic Disk? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46