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

前端 未结 4 1748
一整个雨季
一整个雨季 2020-12-10 20:32

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

相关标签:
4条回答
  • 2020-12-10 20:49

    there is no Like operator in VBScript. You could use 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

    0 讨论(0)
  • 2020-12-10 20:55

    you can use StrComp to have same result in this way

        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 .

    You can replace vbTextCompare with 1 and you will have same result.

    If letter case is important you can use vbBinaryCompare or 0.

    0 讨论(0)
  • 2020-12-10 21:05

    A way to do that with select case. This version of instr() is case sensitive, but other versions aren't. instr() returns the position of the found substring, which here is always one.

    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
    
    0 讨论(0)
  • 2020-12-10 21:07

    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.

    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
    
    0 讨论(0)
提交回复
热议问题