Vbscript VBS使用LIKE比较字符串“;“未定义子功能或功能”;
我正在尝试制作一个脚本,将网络打印机连接到用户计算机。 脚本使用需要打印机的计算机名作为参数 打印机名称与printserver名称相似,例如server_USA有打印机_USA01、打印机_USA02 但它在到达第一个位置时抛出错误“Sub或Function not defined”,如。。。为什么?Vbscript VBS使用LIKE比较字符串“;“未定义子功能或功能”;,vbscript,Vbscript,我正在尝试制作一个脚本,将网络打印机连接到用户计算机。 脚本使用需要打印机的计算机名作为参数 打印机名称与printserver名称相似,例如server_USA有打印机_USA01、打印机_USA02 但它在到达第一个位置时抛出错误“Sub或Function not defined”,如。。。为什么? Set shl = WScript.CreateObject("WScript.Shell") strName = Wscript.Arguments.Item(0) 'input Print
Set shl = WScript.CreateObject("WScript.Shell")
strName = Wscript.Arguments.Item(0)
'input Printer name
strPrinter = InputBox("Please enter share name of printer to install:", _
"Add network printer")
if strPrinter = "" then
msgbox "Can't be empty."
WScript.quit
elseif strPrinter Like "printer_USA*" then
strServer = server_USA
elseif strPrinter Like "printer_SPAIN*" then
strServer = server_SPAIN
else
'Printer name NOT registered, input printserver manually:
strServer = inputbox("Please enter the name of the printserver","printserver")
if strServer = "" then
msgbox "Can't be empty."
WScript.quit
End if
End if
'ADD
shl.run "RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\" & strName & " /n\\" & strServer & "\" & strPrinter
VBScript中没有Like运算符。你可以用
vbTextCompare常量(值=1)用于执行文本比较您可以使用StrComp以这种方式获得相同的结果
If StrComp(strPrinter,"printer_USA",vbTextCompare)=0 then
strServer = server_USA
End IF
相等0
表示strprenter
和
之间的差异为零,忽略字母大小写,因为我们使用vbTextCompare
您可以将vbTextCompare
替换为1
,您将得到相同的结果
如果字母大小写很重要,您可以使用
vbBinaryCompare
或0
选择大小写。此版本的instr()区分大小写,但其他版本则不区分大小写。instr()返回找到的子字符串的位置,这里始终是一个
select case 1
case instr(strPrinter, "") + 1
wscript.echo "empty"
case instr(strPrinter, "printer_USA")
wscript.echo "server_USA"
case instr(strPrinter, "printer_SPAIN")
wscript.echo "server_SPAIN"
case instr(strPrinter, "printer_ITALY"), instr(strPrinter, "printer_RUSSIA")
wscript.echo "other known ones"
case else
wscript.echo "not registered"
end select
我使用了以下替代方法(VBScript正则表达式)… 使用与LIKE稍有不同的语法,但最简单的解决方案可以成功匹配LIKE运算符
dim regExp
set regExp=CreateObject("VBScript.RegExp")
regExp.IgnoreCase = true
regExp.Global = true
regxp.Pattern = ".*Test Pattern.*" ' example only, basic pattern
if regExp.Test(MyString) then
' match successful
end if
据我所知,VBS中不支持
Like
运算符。用InStr试试吧
dim regExp
set regExp=CreateObject("VBScript.RegExp")
regExp.IgnoreCase = true
regExp.Global = true
regxp.Pattern = ".*Test Pattern.*" ' example only, basic pattern
if regExp.Test(MyString) then
' match successful
end if