Batch not-equal (inequality) operator

483,713

Solution 1

Try

if NOT "asdf" == "fdas" echo asdf

Solution 2

Use NEQ instead.

if "asdf" NEQ "fdas" echo asdf

Solution 3

I know this is quite out of date, but this might still be useful for those coming late to the party. (EDIT: updated since this still gets traffic and @Goozak has pointed out in the comments that my original analysis of the sample was incorrect as well.)

I pulled this from the example code in your link:

IF !%1==! GOTO VIEWDATA
REM  IF NO COMMAND-LINE ARG...
FIND "%1" C:\BOZO\BOOKLIST.TXT
GOTO EXIT0
REM  PRINT LINE WITH STRING MATCH, THEN EXIT.

:VIEWDATA
TYPE C:\BOZO\BOOKLIST.TXT | MORE
REM  SHOW ENTIRE FILE, 1 PAGE AT A TIME.

:EXIT0

!%1==! is simply an idiomatic use of == intended to verify that the thing on the left, that contains your variable, is different from the thing on the right, that does not. The ! in this case is just a character placeholder. It could be anything. If %1 has content, then the equality will be false, if it does not you'll just be comparing ! to ! and it will be true.

!==! is not an operator, so writing "asdf" !==! "fdas" is pretty nonsensical.

The suggestion to use if not "asdf" == "fdas" is definitely the way to go.

Solution 4

Try:

if not "asdf" == "fdas" echo asdf

That works for me on Windows XP (I get the same error as you for the code you posted).

Solution 5

NEQ is usually used for numbers and == is typically used for string comparison.

I cannot find any documentation that mentions a specific and equivalent inequality operand for string comparison (in place of NEQ). The solution using IF NOT == seems the most sound approach. I can't immediately think of a circumstance in which the evaluation of operations in a batch file would cause an issue or unexpected behavior when applying the IF NOT == comparison method to strings.

I wish I could offer insight into how the two functions behave differently on a lower level - would disassembling separate batch files (that use NEQ and IF NOT ==) offer any clues in terms of which (unofficially documented) native API calls conhost.exe is utilizing?

Share:
483,713

Related videos on Youtube

ripper234
Author by

ripper234

See blog or LinkedIn Profile

Updated on December 11, 2021

Comments

  • ripper234
    ripper234 over 2 years

    According to this, !==! is the not-equal string operator. Trying it, I get:

    C:\> if "asdf" !==! "fdas" echo asdf
    !==! was unexpected at this time.
    

    What am I doing wrong?

  • Anders
    Anders over 14 years
    this requires command extensions to be turned on (They are by default on 2000+ but can be turned off system wide or as a parameter to cmd.exe) Normally you should turn them on with setlocal, but for a simple if not equal test, just use "if not", it goes back to the good old DOS days
  • user66001
    user66001 almost 11 years
    Thanks jatrim.. Most definitely helpful for those searching for an answer, rather than asking the question again. It isn't like StackExchange locks off the ability to answer/comment due to age of questions, so think this is fully acceptable.
  • Goozak
    Goozak over 10 years
    The ! are used here in case %1 is empty, thus resulting in the test !==!, which is true. You could use %1.==. instead (almost any character would do) — the purpose being to make sure that both sides of the equality test has something to test. The !==! notation is definitely NOT a not-equal sign. Better stick with if not ....
  • Jouni Aro
    Jouni Aro over 2 years
    Yeah, I usually use 'if X%1 == X goto somewhere' to check if the script has no arguments. It's the same as 'if "%1" == "" goto somewhere', except that will fail in batch files, because "" evaluates to nothing and the whole sentence reduces to 'if %1 == goto somewhere'. I prefer X, since ! makes you think it's a special operator, whereas it's just used as an extra character to overcome the problem with an empty string. Batch files are relics from the 1980's...