How to check the value of wscript.arguments(0) without two if statements

StackOverflow https://stackoverflow.com/questions/20262724

  •  06-08-2022
  •  | 
  •  

質問

I want to be able to check the value of the first passed argument to a windows script. But I want to be able to do it in such a way that the script will not give a runtime error if there are no arguments passed.

This is only a matter of curiosity as I am able get my script working with two if statements, but I want to know if there's a way to do it with just one (like

monthly = false

if wscript.arguments.count > 0 then 

    if wscript.arguments(0) = "monthly" then

        monthly = true

    end if

end if

It would be neater if this could be done...

if wscript.arguments.count > 0 and wscript.arguments(0) = "Monthly" then

    monthly = true

end if

But that gives a subscript out of range error because the scripting engine is trying to check the value of an array item that doesn't exist.

I know I can do this type of check in PHP (if(isset($_POST['somevariable'])) && $_POST['somevariable'] == 'somevalue')

役に立ちましたか?

解決

The answer is you can't do it because VBScript has no short-circuit and operator.

Since this is out of curiousity, you can check these links :

http://en.wikipedia.org/wiki/Short-circuit_evaluation

VBScript conditional short-circuiting workaround

他のヒント

If there is more than one test for the argument, you don't have to keep repeating the double if/then conditions.

Test once for the argument and put it in a variable. All other checks after that only need one if/then against the variable. Or use 'case' to select.

dim argument
if wscript.arguments.count > 0 then
    argument = wscript.arguments(0)
end if

if argument = "test1" then
  msgbox "test1"
end if

if argument = "test2" then
  msgbox "test2"
end if
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top