[英]Type Mismatch Using Instr
嗨,希望你能帮我解决我的问题,我已经使用 vbs 为 IE10 创建了一个包,它会检查计算机上是否安装了修补程序,如果缺少一个修补程序/更新,脚本将中止安装并记录返回码。
我在这里有一个用于检查计算机上安装的修补程序的代码,但我仍然遇到错误类型与使用“Instr”不匹配的问题
$--------------------------------检查已安装的补丁 ----------- -----------------------
objShell.run "cmd /c " & chr(34) & "systeminfo >" & strWin & "\Temp\IE10_patches.txt" & chr(34), 0, True
msgbox strWin & "\Temp\IE10_patches.txt"
strFile = strWin & "\Temp\IE10_patches.txt"
WScript.Sleep 5000
Const ForReading = 1
Set objFileToRead = objFSO.OpenTextFile(strFile, ForReading)
Do Until objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine
If Len(strLine) > 0 Then
strText = Trim(strLine)
'strLen = Len(strLine)
'msgbox strLen
'MSGBOX strLine
'ctr = ctr+ 1
'MSGBOX ctr
在这里得到一个错误 - > If (InStr(strText, "KB2786081")) > 0 Then
KB2786081_Flag = "True"
ElseIf (InStr(strText, "KB2731771")) > 0 Then
KB2731771_Flag = "True"
ElseIf (InStr(strText, "KB2729094")) > 0 Then
KB2729094_Flag = "True"
ElseIf (InStr(strText, "KB2639308")) > 0 Then
KB2639308_Flag = "True"
ElseIf (InStr(strText, "KB2533623")) > 0 Then
KB2533623_Flag = "True"
ElseIf (InStr(strText, "KB2670838")) > 0 Then
KB2670838_Flag = "True"
End If
End If
loop
objFileToRead.Close
在今天自己遇到这个问题然后意识到我做了什么之后,我可能有解决方案。
在我的代码中的某个地方,我使用了instr
作为变量名,然后搞砸了InStr()
函数赋值,产生了错误
Microsoft VBScript 运行时 (0x800A000D)
类型不匹配:'instr'
因为InStr
现在是String
而不是函数声明。
为了解决这个问题,只需将我的变量赋值更改为Dim in_str
。
通常你不能这样做,因为InStr()
是一个函数,如果我没有先声明instr
并试图将它分配给一个 String 会得到这个错误
Microsoft VBScript 运行时 (0x800A01F5)
非法赋值:'instr'
只有当你像这样首先声明instr
时才会发生这种情况
'Key is this line without the declaration instr = "test"
'would fail with an Illegal assignment runtime error.
Dim instr
instr = "test"
'This line will trigger Type Mismatch error if instr has been declared.
If Instr(1, string1, string2) > 0 Then
...
End If
为了找到我自己的问题的解决方案,我稍微清理了脚本,以下内容在 Windows 10 上对我strWin
。(没有strWin
和chr(34)
以及其他一些小改动,基本相同。我的电脑不会有任何这些 Windows 更新,但如果它是一个问题,仍然会抛出类型不匹配错误):
Const ForReading = 1
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.run "cmd /c systeminfo > IE10_patches.txt", 0, True
strFile = "IE10_patches.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToRead = objFSO.OpenTextFile(strFile, ForReading)
Do While Not objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine
If Len(strLine) > 0 Then
strText = Trim(strLine)
wscript.echo strText
End If
If (InStr(strText, "KB2786081")) > 0 Then
KB2786081_Flag = "True"
ElseIf (InStr(strText, "KB2731771")) > 0 Then
KB2731771_Flag = "True"
ElseIf (InStr(strText, "KB2729094")) > 0 Then
KB2729094_Flag = "True"
ElseIf (InStr(strText, "KB2639308")) > 0 Then
KB2639308_Flag = "True"
ElseIf (InStr(strText, "KB2533623")) > 0 Then
KB2533623_Flag = "True"
ElseIf (InStr(strText, "KB2670838")) > 0 Then
KB2670838_Flag = "True"
End If
Loop
objFileToRead.Close
在纠结这个的过程中,我想起了我自己的instr
Type Mismatch 错误的原因。 使用instr
,如果要使用比较(0 = vbBinaryCompare(默认)或 1 = vbTextCompare),则必须使用 Start 选项。 所以在这个例子中(和我的错误),如下:
If (InStr(strText, "KB2786081", 1))
需要是:
If (InStr(1, strText, "KB2786081", 1))
在这种情况下,当简单地将 instr 用作布尔值时,也不需要> 0
。 (在原始 OP 的脚本中也不应该需要它。)
尝试这个
If (InStr(strText, "KB2786081",1)) > 0 Then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.