Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch not-equal (inequality) operator

People also ask

How do you write not equal to in batch file?

According to this, ! ==! is the not-equal string operator.

What is || in batch script?

|| (not used above) executes this command only if previous command's errorlevel is NOT 0 > output to a file >> append output to a file < input from a file | output of one command into the input of another command ^ escapes any of the above, including itself, if needed to be passed to a program " parameters with spaces ...

Does batch have else if?

However, you can't use else if in batch scripting. Instead, simply add a series of if statements: if %x%==5 if %y%==5 (echo "Both x and y equal 5.") if %x%==10 if %y%==10 (echo "Both x and y equal 10.") else (echo "Invalid input.")

How do I exit a batch script?

EXIT /B at the end of the batch file will stop execution of a batch file. Use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.


Try

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

Use NEQ instead.

if "asdf" NEQ "fdas" echo asdf

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.


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