37

I'm trying to do two conditions on a single If statement in vbscript. Should be really simple, but it's not working. Something like:

 If Not (fileName = testFileName) & (fileName <> "") Then
Else ....

I'm making it two if statements to get it working, but can I do a not conditional with an "and" with another not condition?

3 Answers 3

67

Use the 'And' keyword for a logical and. Like this:

If Not ((filename = testFileName) And (fileName <> "")) Then
6
  • Thank you, that is what I was trying to do! Actually, the Not keyword doesn't work. This does: If ((fileName <> objErrFileName) And (fileName <> "")) Then Commented Sep 20, 2011 at 18:33
  • @Hogan: whoops, with that added set of parentheses, you changed the logic! Commented Sep 21, 2011 at 11:15
  • @Jean-FrançoisCorbett I think it's to control operator precedence
    – codaamok
    Commented Mar 7, 2016 at 10:47
  • 1
    @adampski: So do I, but who knows whether the OP didn't want it the other way around? Commented Mar 7, 2016 at 11:16
  • 1
    @Jean-FrançoisCorbett - Just to be clear -- I changed to logic to what was clearly the OP's intention "if file is not empty and does not equal testFileName" The other possible meaning ("file is not empty and equals testFileName") would make no sense since that is the same as "equals testFileName".
    – Hogan
    Commented Mar 7, 2016 at 16:55
9

As Hogan notes above, use an AND instead of &. See this tutorial for more info.

0

Here are the codes to check multiple IF in VBS:

Function isFlagTrue(line)
    ' wscript.echo "line : "& line
    Dim returnValue, key, value, result
    key = "SHOULD_SEND_EMAIL"
    value="TRUE"
    If InStr(line, key) > 0 And InStr(line, value) > 0 Then
        returnValue = True
    Else
        returnValue = False
    End If
    ' wscript.echo "HELLO : "& returnValue
    isFlagTrue = returnValue
End Function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.