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?

KatieK
  • 13,586
  • 17
  • 76
  • 90
James Drinkard
  • 15,342
  • 16
  • 114
  • 137

3 Answers3

66

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

If Not ((filename = testFileName) And (fileName <> "")) Then
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • 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 – James Drinkard Sep 20 '11 at 18:33
  • @Hogan: whoops, with that added set of parentheses, you changed the logic! – Jean-François Corbett Sep 21 '11 at 11:15
  • @Jean-FrançoisCorbett I think it's to control operator precedence – codaamok Mar 07 '16 at 10:47
  • 1
    @adampski: So do I, but who knows whether the OP didn't want it the other way around? – Jean-François Corbett Mar 07 '16 at 11:16
  • @Jean-FrançoisCorbett True, hopefully now whoever reads this in future knows the purpose of extra parathese and can decide with that in mind – codaamok Mar 07 '16 at 11:23
  • 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 Mar 07 '16 at 16:55
9

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

JW8
  • 1,496
  • 5
  • 21
  • 36
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
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79