简体   繁体   English

VBS 使用 LIKE 来比较字符串“Sub or Function not defined”

[英]VBS using LIKE to compare strings “Sub or Function not defined”

I'm trying to make a script to connect a network printer to a user computer.我正在尝试制作一个脚本来将网络打印机连接到用户计算机。 The script uses the computer name who needs the printer as a parameter.该脚本使用需要打印机的计算机名称作为参数。

Printers names are similar their printserver name, eg.打印机名称与其打印服务器名称相似,例如。 server_USA has printers like printer_USA01, printer_USA02. server_USA 有像 printer_USA01、printer_USA02 这样的打印机。

But it's throwing the error "Sub or Function not defined" when arrives at the first like... why ?但是它在到达第一个时抛出错误“Sub or Function not defined”......为什么?

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

there is no Like operator in VBScript. VBScript 中没有 Like 运算符。 You could use Instr .你可以使用Instr

if strPrinter = "" then
    msgbox "Can't be empty."
    WScript.quit

elseif Instr( 1, strPrinter, "printer_USA", vbTextCompare ) > 0 then
    strServer = server_USA

The vbTextCompare constant ( value=1) is used to Perform a textual comparison vbTextCompare 常量(值=1)用于执行文本比较

you can use StrComp to have same result in this way您可以使用StrComp以这种方式获得相同的结果

    If StrComp(strPrinter,"printer_USA",vbTextCompare)=0 then  
    strServer = server_USA
    End IF

equal 0 mean zero different between strPrinter and printer_USA with ignore the letter case because we use vbTextCompare .等于0意味着strPrinterprinter_USA之间的差异strPrinter ,因为我们使用vbTextCompare忽略字母大小写。

You can replace vbTextCompare with 1 and you will have same result.您可以将vbTextCompare替换为1 ,您将获得相同的结果。

If letter case is important you can use vbBinaryCompare or 0 .如果字母大小写很重要,您可以使用vbBinaryCompare0

A way to do that with select case.一种使用 select case 做到这一点的方法。 This version of instr() is case sensitive, but other versions aren't.此版本的 instr() 区分大小写,但其他版本不区分大小写。 instr() returns the position of the found substring, which here is always one. 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

I used the following alternative (VBScript Regular Expressions)… Uses slightly different syntax from LIKE but easiest solution to make a match successfully similar to LIKE operator.我使用了以下替代方案(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM