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 - Type mismatch: FormatNumber

Reply
 
Old 11-07-2008   #1 (permalink)
Larry


 
 

Type mismatch: FormatNumber

I want to determine the amount of free space on the hard drives in terms of
Mega Bytes and format the output with digit grouping. My script keeps
returning the error "Type mismatch: FormatNumber" and points to the line with
the Echo. I can't figure out why the FormatNumber doesn't work.

.... relevant code
Const CONVERSION_FACTOR = 1048576
FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
WScript.Echo MessageStart & FormatNumber(FreeMegaBytes,0,0,0-2) & _
MessageEnd & objLogicalDisk.DeviceID



My System SpecsSystem Spec
Old 11-07-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: Type mismatch: FormatNumber


"Larry" <Larry@xxxxxx> wrote in message
news:B707B153-C9EF-4866-94E8-685F67EF4128@xxxxxx
Quote:

>I want to determine the amount of free space on the hard drives in terms of
> Mega Bytes and format the output with digit grouping. My script keeps
> returning the error "Type mismatch: FormatNumber" and points to the line
> with
> the Echo. I can't figure out why the FormatNumber doesn't work.
>
> ... relevant code
> Const CONVERSION_FACTOR = 1048576
> FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
> WScript.Echo MessageStart & FormatNumber(FreeMegaBytes,0,0,0-2) & _
> MessageEnd & objLogicalDisk.DeviceID
>
There is nothing wrong with the FormatNumber statement. Unfortunately your
"relevant code" omits several things which are relevant, e.g. your
LogicalDisk object and the various string variables (?) in your echo
statement.


My System SpecsSystem Spec
Old 11-07-2008   #3 (permalink)
Larry


 
 

Re: Type mismatch: FormatNumber

Sorry, I'm not familiar with code posting etiquette and I'm new to scripting.
I left those things out because the script runs just fine until I try to
format the number and the error points to the line with the FormatNumber.
Here's the full code:

Option Explicit

Dim Computers, Computer
Computers = Array(".")

Dim MessageStart
MessageStart = "There are "

Dim MessageEnd
MessageEnd = " MegaBytes of free disk space on "

Dim colLogicalDisk, i, FreeMegaBytes, objLogicalDisk, objWMIService

Const CONVERSION_FACTOR = 1048576
Const ONE_SECOND = 1000
Const ONE_MINUTE = 60000
Const ONE_HOUR = 3600000
Const WARNING_THRESHOLD = 10000

WScript.Echo "My computer name is " & Computers(0)
For i = 1 To 1
For Each Computer In Computers
Set objWMIService = GetObject("WinMgmtS://" & Computer)
Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk")
WScript.Echo "There are " & colLogicalDisk.Count & " logical
drives on the computer " & Computer & "."
For Each objLogicalDisk In colLogicalDisk
FreeMegaBytes = objLogicalDisk.FreeSpace /
CONVERSION_FACTOR
WScript.Echo MessageStart &
FormatNumber(FreeMegaBytes,0,0,0-2) & MessageEnd & objLogicalDisk.DeviceID
If FreeMegaBytes < WARNING_THRESHOLD Then
WScript.Echo vbTab & "The " &
objLogicalDisk.DeviceID & " on " & Computer & " is low on disk space."
Else
WScript.Echo vbTab & "The " & objLogicalDisk.DeviceID & " on " &
Computer & " has adequate disk space."
End If
Next
WScript.Echo vbCr
Next
WScript.Sleep ONE_SECOND
Next


"Pegasus (MVP)" wrote:
Quote:

>
> "Larry" <Larry@xxxxxx> wrote in message
> news:B707B153-C9EF-4866-94E8-685F67EF4128@xxxxxx
Quote:

> >I want to determine the amount of free space on the hard drives in terms of
> > Mega Bytes and format the output with digit grouping. My script keeps
> > returning the error "Type mismatch: FormatNumber" and points to the line
> > with
> > the Echo. I can't figure out why the FormatNumber doesn't work.
> >
> > ... relevant code
> > Const CONVERSION_FACTOR = 1048576
> > FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
> > WScript.Echo MessageStart & FormatNumber(FreeMegaBytes,0,0,0-2) & _
> > MessageEnd & objLogicalDisk.DeviceID
> >
>
> There is nothing wrong with the FormatNumber statement. Unfortunately your
> "relevant code" omits several things which are relevant, e.g. your
> LogicalDisk object and the various string variables (?) in your echo
> statement.
>
>
>
My System SpecsSystem Spec
Old 11-07-2008   #4 (permalink)
Richard Mueller [MVP]


 
 

Re: Type mismatch: FormatNumber

The FreeSpace property is missing for CD drives (for example) that have no
CD. Perhaps you can code similar to:

FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
If IsNull(FreeMegaBytes) Then
FreeMegaBytes = 0
End If

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"Larry" <Larry@xxxxxx> wrote in message
news:93218821-0337-4C3A-8B62-11340AA8E009@xxxxxx
Quote:

> Sorry, I'm not familiar with code posting etiquette and I'm new to
> scripting.
> I left those things out because the script runs just fine until I try to
> format the number and the error points to the line with the FormatNumber.
> Here's the full code:
>
> Option Explicit
>
> Dim Computers, Computer
> Computers = Array(".")
>
> Dim MessageStart
> MessageStart = "There are "
>
> Dim MessageEnd
> MessageEnd = " MegaBytes of free disk space on "
>
> Dim colLogicalDisk, i, FreeMegaBytes, objLogicalDisk, objWMIService
>
> Const CONVERSION_FACTOR = 1048576
> Const ONE_SECOND = 1000
> Const ONE_MINUTE = 60000
> Const ONE_HOUR = 3600000
> Const WARNING_THRESHOLD = 10000
>
> WScript.Echo "My computer name is " & Computers(0)
> For i = 1 To 1
> For Each Computer In Computers
> Set objWMIService = GetObject("WinMgmtS://" & Computer)
> Set colLogicalDisk =
> objWMIService.InstancesOf("Win32_LogicalDisk")
> WScript.Echo "There are " & colLogicalDisk.Count & " logical
> drives on the computer " & Computer & "."
> For Each objLogicalDisk In colLogicalDisk
> FreeMegaBytes = objLogicalDisk.FreeSpace /
> CONVERSION_FACTOR
> WScript.Echo MessageStart &
> FormatNumber(FreeMegaBytes,0,0,0-2) & MessageEnd & objLogicalDisk.DeviceID
> If FreeMegaBytes < WARNING_THRESHOLD Then
> WScript.Echo vbTab & "The " &
> objLogicalDisk.DeviceID & " on " & Computer & " is low on disk space."
> Else
> WScript.Echo vbTab & "The " & objLogicalDisk.DeviceID & " on "
> &
> Computer & " has adequate disk space."
> End If
> Next
> WScript.Echo vbCr
> Next
> WScript.Sleep ONE_SECOND
> Next
>
>
> "Pegasus (MVP)" wrote:
>
Quote:

>>
>> "Larry" <Larry@xxxxxx> wrote in message
>> news:B707B153-C9EF-4866-94E8-685F67EF4128@xxxxxx
Quote:

>> >I want to determine the amount of free space on the hard drives in terms
>> >of
>> > Mega Bytes and format the output with digit grouping. My script keeps
>> > returning the error "Type mismatch: FormatNumber" and points to the
>> > line
>> > with
>> > the Echo. I can't figure out why the FormatNumber doesn't work.
>> >
>> > ... relevant code
>> > Const CONVERSION_FACTOR = 1048576
>> > FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
>> > WScript.Echo MessageStart & FormatNumber(FreeMegaBytes,0,0,0-2) & _
>> > MessageEnd & objLogicalDisk.DeviceID
>> >
>>
>> There is nothing wrong with the FormatNumber statement. Unfortunately
>> your
>> "relevant code" omits several things which are relevant, e.g. your
>> LogicalDisk object and the various string variables (?) in your echo
>> statement.
>>
>>
>>

My System SpecsSystem Spec
Old 11-07-2008   #5 (permalink)
Larry


 
 

Re: Type mismatch: FormatNumber

I noticed that problem and was going to figure out how to deal with it later.
I see what you're saying though. The value comes back as "Null" for those CD
drives and FormatNumber doesn't know what to do that value so that's what's
actually causing my problem. Awesome. Thanks for the help.

"Richard Mueller [MVP]" wrote:
Quote:

> The FreeSpace property is missing for CD drives (for example) that have no
> CD. Perhaps you can code similar to:
>
> FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
> If IsNull(FreeMegaBytes) Then
> FreeMegaBytes = 0
> End If
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
> "Larry" <Larry@xxxxxx> wrote in message
> news:93218821-0337-4C3A-8B62-11340AA8E009@xxxxxx
Quote:

> > Sorry, I'm not familiar with code posting etiquette and I'm new to
> > scripting.
> > I left those things out because the script runs just fine until I try to
> > format the number and the error points to the line with the FormatNumber.
> > Here's the full code:
> >
> > Option Explicit
> >
> > Dim Computers, Computer
> > Computers = Array(".")
> >
> > Dim MessageStart
> > MessageStart = "There are "
> >
> > Dim MessageEnd
> > MessageEnd = " MegaBytes of free disk space on "
> >
> > Dim colLogicalDisk, i, FreeMegaBytes, objLogicalDisk, objWMIService
> >
> > Const CONVERSION_FACTOR = 1048576
> > Const ONE_SECOND = 1000
> > Const ONE_MINUTE = 60000
> > Const ONE_HOUR = 3600000
> > Const WARNING_THRESHOLD = 10000
> >
> > WScript.Echo "My computer name is " & Computers(0)
> > For i = 1 To 1
> > For Each Computer In Computers
> > Set objWMIService = GetObject("WinMgmtS://" & Computer)
> > Set colLogicalDisk =
> > objWMIService.InstancesOf("Win32_LogicalDisk")
> > WScript.Echo "There are " & colLogicalDisk.Count & " logical
> > drives on the computer " & Computer & "."
> > For Each objLogicalDisk In colLogicalDisk
> > FreeMegaBytes = objLogicalDisk.FreeSpace /
> > CONVERSION_FACTOR
> > WScript.Echo MessageStart &
> > FormatNumber(FreeMegaBytes,0,0,0-2) & MessageEnd & objLogicalDisk.DeviceID
> > If FreeMegaBytes < WARNING_THRESHOLD Then
> > WScript.Echo vbTab & "The " &
> > objLogicalDisk.DeviceID & " on " & Computer & " is low on disk space."
> > Else
> > WScript.Echo vbTab & "The " & objLogicalDisk.DeviceID & " on "
> > &
> > Computer & " has adequate disk space."
> > End If
> > Next
> > WScript.Echo vbCr
> > Next
> > WScript.Sleep ONE_SECOND
> > Next
> >
> >
> > "Pegasus (MVP)" wrote:
> >
Quote:

> >>
> >> "Larry" <Larry@xxxxxx> wrote in message
> >> news:B707B153-C9EF-4866-94E8-685F67EF4128@xxxxxx
> >> >I want to determine the amount of free space on the hard drives in terms
> >> >of
> >> > Mega Bytes and format the output with digit grouping. My script keeps
> >> > returning the error "Type mismatch: FormatNumber" and points to the
> >> > line
> >> > with
> >> > the Echo. I can't figure out why the FormatNumber doesn't work.
> >> >
> >> > ... relevant code
> >> > Const CONVERSION_FACTOR = 1048576
> >> > FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
> >> > WScript.Echo MessageStart & FormatNumber(FreeMegaBytes,0,0,0-2) & _
> >> > MessageEnd & objLogicalDisk.DeviceID
> >> >
> >>
> >> There is nothing wrong with the FormatNumber statement. Unfortunately
> >> your
> >> "relevant code" omits several things which are relevant, e.g. your
> >> LogicalDisk object and the various string variables (?) in your echo
> >> statement.
> >>
> >>
> >>
>
>
>
My System SpecsSystem Spec
Old 11-07-2008   #6 (permalink)
Pegasus \(MVP\)


 
 

Re: Type mismatch: FormatNumber


"Larry" <Larry@xxxxxx> wrote in message
news:93218821-0337-4C3A-8B62-11340AA8E009@xxxxxx
Quote:

> Sorry, I'm not familiar with code posting etiquette and I'm new to
> scripting.
> I left those things out because the script runs just fine until I try to
> format the number and the error points to the line with the FormatNumber.
> Here's the full code:
It's not really a question of etiquette but of posting sufficient details to
enable respondents to see the cause of the problem. Some people post
hundreds of lines of code, which is bad because respondents cannot really be
expected to wade through the lot. You posted too little, leaving out the
essential code segment as you saw later on.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Type mismatch:'alert' vbscript to compact access db VB Script
Date Type mismatch in criteria expression VB Script
Data type mismatch in criteria expression (with Request.form) VB Script
Data type mismatch in criteria expression VB Script
HTA: type mismatch (runtime) on sub call in OnChange VB Script


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