0

I seem to have a problem with my "if" statements. Currently after the pause in "start" the file just closes and nothing else happens. Chances are, I have no idea what I'm doing.

@echo off

set startnum=0
goto start

:start
pause
set startnum="!startnum!"+1
if "%startnum%"="%0%" goto fail
if "%startnum%"="%1%" goto success
goto start

:success
cls
echo It worked!
echo "%startnum%"
pause.
exit

:fail
cls
echo Failure
pause.
exit

2 Answers2

1

First problem:

set startnum="!startnum!"+1

Evidently, you wish to add 1 to startnum.

Your set command would set startnum to "!startnum!"+1. Literally. To perform arithmetic, you need set /a.

set /A startnum="!startnum!"+1

well, this won't work as "!startnum! isn't numeric. Had you invoked delayedexpansion beforehand, then the value of startnum would have been substituted for !startnum! yielding set /A startnum="0"+1 which makes more, but still not much sense.

set /A startnum=startnum+1

adds 1 to startnum - see set /? from the prompt for documentation.

set /A startnum+=1

would also add 1 to startnum.

Next problem.

if "%startnum%"="%0%" goto fail

Well, you appear to have found lss and all that gang. Problem is that the simple comparison operator is ==, not =.

if "%startnum%"=="%0%" goto fail

Now - what will that do? It will compare "thecontentsofstartnum" to "thecontentsof0". Since both of these arguments are quoted, batch will perform a string comparison. With a string comparison, 123 is less than 89 because 1 is less than 8.

But - you are attempting an equivalence comparison (equ as the operator may be used instead of ==) so the preceding point is simply AAMOI.

The difficulty is %0% which you may believe attempts to extract the value of the variable 0 but actually it replaces %0 with the value of the 0th parameter to the batchfile, which is the batchfile name itself, so you get "batchfilename%" - probably not what you actually wanted.

if "%startnum%"=="0" goto fail

is the way to implement that test.

Magoo
  • 68,705
  • 7
  • 55
  • 76
0

The first IF statement is preprocessed by cmd.exe to

if ""!startnum!"+1"="test.bat" goto fail

which is a completely invalid IF condition.

cmd.exe outputs a syntax error message because of "="test.bat"" and exits batch file processing. This can be seen by debugging the batch file.

The solution is using right syntax for

  • assigning a value to an environment variable,
  • an arithmetic expression,
  • and last but not least the IF condition itself.

The batch file code fixed:

@echo off

set "startnum=0"
goto Begin

:Begin
set /A startnum+=1
if "%startnum%" == "0" goto Fail
if "%startnum%" == "1" goto Success
goto Begin

:Success
cls
echo It worked!
echo "%startnum%"
pause
exit /B

:Fail
cls
echo Failure
pause
exit /B

It would be safe here to remove all double quotes on both IF conditions.

One more hint: Don't use the name of a command like START as label. It works, but it should be avoided in case of ever adding to batch file the command START and searching for either command or label.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains how to reference batch file arguments.
  • cls /?
  • echo /?
  • exit /?
  • goto /?
  • if /?
  • pause /?
  • set /?

Further read the answers on following questions:

Mofi
  • 38,783
  • 14
  • 62
  • 115