0

我有一个 vbscript 的“问题”。我检查 cd 是否加载了这段代码:

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

它返回“驱动器:假”

但是,如果我更改打印输出的行:

WScript.Echo SubItems.MediaLoaded

返回 -1

有人知道我怎么能得到 "DRIVE:-1" 吗?

我用 WScript.Echo "DRIVE: "&cint(SubItems.MediaLoaded) 解决了感谢 madzone

4

3 回答 3

1

我的英语不好,我认为 Ekkehard Horner 的解释比我的好。我所知道的是字符串连接将Boolean数据类型转换为文字形式。

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
于 2013-05-24T21:00:11.680 回答
0

使用 CInt 函数将字符串转换回整数。

WScript.Echo "Drive: " & CInt(SubItems.MediaLoaded)
于 2013-05-24T16:46:12.080 回答
0

根据文档,.MediaLoaded 的类型是布尔值

媒体加载

Data type: boolean
Access type: Read-only

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

连接运算符 & 正确处理 VBScript 的(子)类型:

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

WScript.Echo 的内部字符串化组件不会:

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

可能 & 运算符在内部调用 CStr() :

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

Join 函数也很勤奋:

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

当然你有权让你的输出看起来像

DRIVE: 0
DRIVE: -1

代替:

MediaLoaded: False
MediaLoaded: True

但指责辛勤工作和改变任何事情是不公平的。

于 2013-05-24T18:09:07.127 回答