Re: How to Get the Total Disk Space on a server

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



'CDbl()' converts a variable to the "Double" datatype. See this link:

http://msdn.microsoft.com/library/en-us/script56/html/vsfctcdbl.asp

The 'oItem.Size' has a data type of 'String', so when you set your variant
variable to 'oItem.Size', it became a string. If you add 2 variables
containing the value of '1' in some numeric datatype together, you will get
a value of '2'. If you add 2 variables containing the value of '1' in the
"String" datatype together, you will get a value of '11'. It simply
concatenates them. Specifically converting your variables to the "Double"
datatype prevents the automatic conversion to the "String" datatype.
Alternately, you could convert the 'oItem.Size' before assigning it to your
variables and you would get the same results:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk
where DriveType=3")

For Each oItem In dskItems
TDisk = TDisk + CDbl(oItem.Size)
TFree = TFree + CDbl(oItem.FreeSpace)
TUsed = TUsed + (CDbl(oItem.Size) - CDbl(oItem.FreeSpace))
Next

WScript.Echo "Total Size: " & TDisk & vbCrLf &_
"Total Free: " & TFree & vbCrLf &_
"Total Used: " & TUsed
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you are unsure of a variable's datatype, temporarily add a 'TypeName'
statement into you code. Something along these lines:

WScript.Echo TypeName(oItem.Size)

It will show you what datatype you are working with. This is really handy
for finding out some function returned an array to you when you were
expecting a string.

Most of the time when you work with the "Variant" datatype in VBScript, it
will make the correct assumption on whether to concatenate or add, but not
always.

"MFelkins" <MFelkins@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5DAE12CB-2FBD-4F53-A916-0636D1050099@xxxxxxxxxxxxxxxx
> That works great. Can't say I understand it. What does the CDbl do?
>
> "James Whitlow" wrote:
>
> > "MFelkins" <MFelkins@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> > news:5B8B6D7E-B5BD-43D6-9FB7-58589B4D688D@xxxxxxxxxxxxxxxx
> > > This sample script will pull the Disk Name for every logical disk on a
> > > machine. I need to get the disk size, free space and used for each
disk
> > and
> > > total them up for the agreget disk size for each machine. I would also
> > like
> > > to limit the search to local hard disks , type 3 in Win32_LogicalDisk
> > where
> > > DriveType=3.
> > >
> >
'---------------------------------------<8>---------------------------------
> > -----------------
> > > For Each Disk In GetObject( _
> > > "winmgmts:").InstancesOf ("CIM_LogicalDisk")
> > > WScript.Echo "Instance:", Disk.Path_.Relpath
> > > Next
> > > If Err <> 0 Then
> > > set lasterr = CreateObject("WbemScripting.SWbemLastError")
> > > Wscript.echo lasterr.Operation
> > > End If
> > >
> >
'--------------------------------------<8>----------------------------------
> > ------------------
> > > Here is what I tried and all I get is Zero
> > >
> >
'--------------------------------------<8>----------------------------------
> > ------------------
> > > strComputer = "."
> > > Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
> > > Set dskItems = objWMIService.ExecQuery("SELECT * FROM
Win32_LogicalDisk
> > > where DriveType=3", "WQL", _
> > > wbemFlagReturnImmediately +
> > > wbemFlagForwardOnly)
> > > For Each oItem In dskItems
> > > AgDisk = oItem.name(0)
> > > Select Case AgDisk
> > > Case "C"
> > > CDisk = oItem.size
> > > CUsed = oItem.FreeSpace
> > > Case "E"
> > > EDisk = oItem.size
> > > EFree = oItem.FreeSpace
> > > Case "F"
> > > FDisk = oItem.size
> > > FFree = oItem.FreeSpace
> > > Case "G"
> > > GDisk = oItem.size
> > > GFree = oItem.FreeSpace
> > > Case "I"
> > > IDisk = oItem.size
> > > IFree = oItem.FreeSpace
> > > Case Else
> > > On Error GoTo 0
> > > End Select
> > > TDisk = (CDisk + EDisk + FDisk + GDisk + IDisk)
> > > TFree = (CFree + EFree + FFree + GFree + IFree)
> > > 'TUsed = (oItem.Size - oItem.FreeSpace)
> > > Next
> > >
> > > wscript.echo TDisk
> > > wscript.echo TFree
> >
> > I see one little type in your code. For all of the items in your
> > Select...Case statements, your are assigning oItem.FreeSpace to 'xFree'
> > (where x is the drive letter) except the C: drive, where you are
assigning
> > it to 'CUsed'.
> >
> > If you are wanting the total space for all the local disk & not just
C: &
> > E: - I:, you could change your code to something like this (watch for
> > wrapping):
> >
> > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > strComputer = "."
> > Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
> >
> > Set dskItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk
> > where DriveType=3")
> >
> > For Each oItem In dskItems
> > TDisk = CDbl(TDisk) + oItem.size
> > TFree = CDbl(TFree) + oItem.FreeSpace
> > TUsed = CDbl(TUsed) + (oItem.size - oItem.FreeSpace)
> > Next
> >
> > wscript.echo "Total Size: " & TDisk & vbCrLf &_
> > "Total Free: " & TFree & vbCrLf &_
> > "Total Used: " & TUsed
> > '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> >
> >


.



Relevant Pages

  • Re: How to Get the Total Disk Space on a server
    ... I have a perl script that gets a lot of the disk info I need but I would like ... The 'oItem.Size' has a data type of 'String', so when you set your variant ... "String" datatype together, you will get a value of '11'. ... I need to get the disk size, free space and used for each ...
    (microsoft.public.windows.server.scripting)
  • Help!! Oracle 9.2.0.5.0 wont properly read the *.rsp
    ... #Datatype: String ... #Datatype: Boolean ... #This page shows the current status in the installation. ...
    (comp.databases.oracle.server)
  • Re: GOSUB, the larger picture (was Worldwide known Excellence of GOSUB)
    ... > does not mean that was not a hack. ... Yep, a Byte datatype was indeed needed, and welcome! ... No need to break String, ... It aint bigger Karl, ...
    (microsoft.public.vb.general.discussion)
  • Re: Access Query Right Align
    ... Gary - The DataType is text for all fields. ... Dim hFile As Long ... Dim strPath As String ... Dim strTextLine As String ...
    (microsoft.public.access.queries)
  • Re: Get Data Type
    ... the data types of the resultant columns - similiar to the decisions made by ... If I then needed to find out what datatype these variables were I could use: ... wanted to know if a string represented a positive long integer then I would ...
    (microsoft.public.access.modulesdaovba)