1.

Solve : If not statement?

Answer»

So first of im back after about a year and trying to finish off the game i started in DOS whiles i have some free time over christmas, doubt anyone will remember me but i remember a few of your names hey all.
Ok so basically I was wondering if this statement is correct, I wanted a if not statement with an and.

Here is the code at the moment:
Code: [Select]if not %MonsterName%==- (
set Flag=Y
)

and here is what i want, not sure if this will work tho, been a long time away from dos
Code: [Select]if not %MonsterName%==- (
if not %MonsterName%==Dead (
set Flag=Y
)
)
So what I want it to do is check if the variable MonsterName equals - or Dead, and if so to set Flag to Y
Thanks in advanced and merry christmas!You wrote "what I want it to do is check if the variable MonsterName equals - or Dead, and if so to set Flag to Y".

Your suggested code looks like it will check if MonsterName is not - and not Dead, and if so set Flag to Y.

That is not the same as what you wrote. So could you be a bit more clear about what you want?


Yea sorry, I did mean " not Dead, and not -" so the flag = Y

As in, if the character does have a name set and is not dead then = YBut you can work it out for yourself. Just write a few LINES of experimental code and play around until you get the results you desire.

For example:

test1.bat

@echo off
:LOOP
set Flag=N
set /p MonsterName="(Q to quit) What is MonsterName? "
if "%MonsterName%"=="Q" goto end
if not "%MonsterName%"=="Dead" (
if not "%MonsterName%"=="-" (
set Flag=Y
)
)
echo Flag=%Flag%
goto loop
:end


C:\>test1.bat
(Q to quit) What is MonsterName? John
Flag=Y
(Q to quit) What is MonsterName? +
Flag=Y
(Q to quit) What is MonsterName? -
Flag=N
(Q to quit) What is MonsterName? Dead
Flag=N
(Q to quit) What is MonsterName? Mary
Flag=Y
(Q to quit) What is MonsterName? Q

C:\>

Nice thanks for the help Happy christmas

- edit

Just noticed you put quotation marks around - and Dead. What difference does that have on the code? Quote from: Risen91 on December 23, 2012, 01:54:10 PM

Just noticed you put quotation marks around - and Dead. What difference does that have on the code?

It is a fairly standard practice (or habit) that many batch scripters have.

Consider the following:

if %var%==x echo Hooray
if %1==debug echo %date% %time% script start > debug.txt

Because variables are expanded, if %var% or %1 were undefined (i.e. empty) then they will EXPAND to (literally) nothing and the script will CRASH (bomb out) when those lines are executed.

these...
if ==x echo Hooray
if ==debug echo %date% %time% script start > debug.txt
... are both ILLEGAL lines.

To protect against that ever happening, many people use enclosing symbols such as quotes around both comparison terms of an IF test. They don't have to be quotes, they just have to be the same so these all work

if "%var%"=="CAT" echo Meow
if [%var%]==[CAT] echo Meow
if {%var%}=={CAT} echo Meow
if abc%var%xyz==abcCATxyz echo Meow

@OP: The other important feature of double quotes is that it handles long filename elements like spaces:

This works:

Code: [Select]@echo off
(set var=DARK CAT)

if "%var%"=="DARK CAT" echo Meow

These three don't work.

if [%var%]==[DARK CAT] echo Meow
if {%var%}=={DARK CAT} echo Meow
if abc%var%xyz==abcDARK CATxyz echo Meow

That's why batchers most often use double quotes.


Discussion

No Comment Found