Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a "problem" with a vbscript. I check if a cd is loaded with this piece of code:

On Error Resume Next
Computer = "."
Set OutFile = CreateObject("WScript.Shell")
Const ForAppending = 2
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set WMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
Set Items = WMIService.ExecQuery("Select * from Win32_CDROMDrive WHERE DRIVE = 'F:'",,48)
For Each SubItems in Items
WScript.Echo "DRIVE: "&SubItems.MediaLoaded
Next

It returns "DRIVE: False"

But if I change the line that prints the output for:

WScript.Echo SubItems.MediaLoaded

Returns -1

Someone knows how I could get "DRIVE:-1" ?

I solved with WScript.Echo "DRIVE: "&cint(SubItems.MediaLoaded) thanks to madzone

share|improve this question
    
WScript.Echo "DRIVE: " & str(SubItems.MediaLoaded) –  matzone May 24 '13 at 16:32
    
Thank you for the response, it didn't help, but it gives me some clues to solve it. I do it with cint(SubItems.MediaLoaded) –  user1675150 May 24 '13 at 16:43
    
good luck ..... –  matzone May 24 '13 at 16:54

3 Answers 3

I'm not good in English and I think Ekkehard Horner explanations is better than mine. What I know is that string concatenation turn Boolean data type in their literal form.

WScript.Echo "" & False, 0, False  '>> False 0 0
WScript.Echo "" & True, -1, True   '>> True -1 -1
WScript.Echo 0 = False, 0 = True   '>> -1 0
WScript.Echo -1 = False, -1 = True '>> 0 -1
share|improve this answer
    
correct/+1, but I'd prefer '& displays Booleans in their literal form' over 'turns'. Nothing is changed. –  Ekkehard.Horner May 24 '13 at 21:17
    
@Ekkehard.Horner agreed, you're right about 'turns'. –  seeker May 25 '13 at 11:27

Use the CInt function to convert the string back to an integer.

WScript.Echo "Drive: " & CInt(SubItems.MediaLoaded)
share|improve this answer
    
There is no string involved: -0.49 –  Ekkehard.Horner May 24 '13 at 18:09

According to the docs, .MediaLoaded's type is boolean

MediaLoaded

Data type: boolean
Access type: Read-only

If True, a CD-ROM is in the drive.

The concatenation operator & handles VBScript's (sub) types correctly:

>> bTrue = True
>> bFalse = False
>> n0 = 0
>> nM1 = -1
>> WScript.Echo "&" & bTrue & bFalse & n0 & nM1
>>
&TrueFalse0-1

The internal stringifier component of WScript.Echo does not:

>> WScript.Echo ".Echo ,", bTrue, bFalse, n0, nM1
>>
.Echo , -1 0 0 -1

Probably the & operator calls CStr() internally:

>> WScript.Echo "CStr() ,", CStr(bTrue), CStr(bFalse), CStr(n0), CStr(nM1)
>>
CStr() , True False 0 -1

The Join function is industrious too:

>> WScript.Echo "Join", Join(Array(bTrue, bFalse, n0, nM1))
>>
Join True False 0 -1

Of course you are entitled to have your output look like

DRIVE: 0
DRIVE: -1

instead of:

MediaLoaded: False
MediaLoaded: True

but it's not fair to accuse the hardworking & of changing anything.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.