我对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
返回“DRIVE:False”
但是,如果我更改打印输出的行:
WScript.Echo SubItems.MediaLoaded
返回-1
有人知道我怎么能得到“DRIVE:-1”?
我解决了WScript.Echo“DRIVE:”& cint(SubItems.MediaLoaded)感谢madzone
答案 0 :(得分: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
答案 1 :(得分:0)
使用CInt函数将字符串转换回整数。
WScript.Echo "Drive: " & CInt(SubItems.MediaLoaded)
答案 2 :(得分:0)
根据the docs,.MediaLoaded的类型是boolean
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
可能是& operator在内部调用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
但是指责勤奋的人并不公平。改变任何事情。