ripper234
ripper234

Reputation: 230048

Batch not-equal (inequality) operator

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?

Upvotes: 331

Views: 583129

Answers (6)

jatrim
jatrim

Reputation: 715

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.

Upvotes: 35

The easiest way I found is to:

IF %ERRORLEVEL% NEQ 0 Echo An error was found

or

IF %ERRORLEVEL% GRT 0 goto error1

The operator !==! has not right value, which means that's used only to know whether a variable was set or not.

IF !%1==! GOTO main

Upvotes: 4

Paul
Paul

Reputation: 81

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?

Upvotes: 8

Frank Bollack
Frank Bollack

Reputation: 25166

Try

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

Upvotes: 621

demoncodemonkey
demoncodemonkey

Reputation: 11957

Use NEQ instead.

if "asdf" NEQ "fdas" echo asdf

Upvotes: 138

Dominic Rodger
Dominic Rodger

Reputation: 99771

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).

Upvotes: 28

Related Questions