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