POOJA SINGH
POOJA SINGH

Reputation: 43

IF ELSE not getting executed when i run batch file

I want to set VAR2 and other variables if VAR is already defined, on the basis of below code:

@ECHO ON
SET ABC=IQ
SET VAR=value
ECHO %VAR%
VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 (
    ECHO Unable to enable extensions
)
IF DEFINED VAR (
    IF "%VAR%" == "code" call :Result
    IF "%VAR%" == "code2" call :Result
    IF "%VAR%" == "value" call :Result
    SET VAR2=C:\abc\files
) ELSE (
    SET VAR2=C:\abc\file\pic & ECHO VAR not defined
)
endlocal & SET VAR3=val
:Result
      SET NAME=CODE & SET VAR2=C:\abc\file

When i execute above code i get output:

SET ABC=IQ
SET VAR=valu
ECHO valu
valu
VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 (ECHO Unable to enable extensions )
IF DEFINED VAR (
IF "valu" == "code" call :Result
 IF "valu" == "code2" call :Result
 IF "valu" == "value" call :Result
 SET VAR2=C:\abc\files
) ELSE (SET VAR2=C:\abc\file\pic & ECHO VAR not defined )
endlocal & SET VAR3=val
SET NAME=CODE & SET VAR2=C:\abc\file

BUT when VAR is not defined, even than :RESULT section is getting executed. And Also when VAR != value, even than :RESULT section is getting executed. Any suggestions why it`s so??

Upvotes: 0

Views: 144

Answers (2)

michael_heath
michael_heath

Reputation: 5372

@ECHO ON
SET "ABC=IQ"
SET "VAR=value"
ECHO(%VAR%

VERIFY OTHER 2>nul

SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 (
    ECHO Unable to enable extensions
)

IF DEFINED VAR (
    IF "%VAR%" == "code" call :Result
    IF "%VAR%" == "code2" call :Result
    IF "%VAR%" == "value" call :Result
    SET "VAR2=C:\abc\files"
) ELSE (
    SET "VAR2=C:\abc\file\pic" & ECHO VAR not defined
)

rem Set VAR2, VAR3 and NAME outside the current local scope.
ENDLOCAL & (
    SET "VAR2=%VAR2%"
    SET "VAR3=val"
    SET "NAME=%NAME%"
)

rem Display variables set beginning with VAR and NAME.
SET VAR
SET NAME

rem End the code here to prevent continuing into the following label.
EXIT /B 0

:Result
    SET "NAME=CODE" & SET "VAR2=C:\abc\file"

Added double quotes around the SET command names and values to prevent trailing whitespace being included.

VAR2 and VAR3 set with same parsed code as ENDLOCAL so they are available in the global scope. This works as the parsed code replaces %VAR2% with it's value before the execution of SETLOCAL and the assignment done by the SET commands.

Added EXIT /B 0 to end the code so that :Result is not executed at the end of the code.

Output of SET VAR:

VAR=value
VAR2=C:\abc\files
VAR3=val

If VAR is undefined by removing the line SET "VAR=value":

VAR2=C:\abc\file\pic
VAR3=val

Many of the commands have help information so can use command /?. Command can be example set /?.

Upvotes: 1

Antares
Antares

Reputation: 671

You should move your :Result block outside of the if blocks (just above :END would be a suitable spot for example; just put a goto End before the :Resultblock to not unwanted run into this). And then call :Result not goto Result inside the if block.
For further documentation execute call /? on the command line (and maybe goto /? for reference). This works with all other commands as well, set /?, if /? for example, and so on.

Since I cannot see where you set a value to VAR your current condition if defined VAR will always be false. So I think your script needs more care than moving the Result block. Or it is just a simplified example, then nevermind ;)
Maybe put an echo %VAR% before the if condition for debugging and to verify that there is a value.

For debugging purposes, try an @ECHO ON in the first line. That gives you all the steps your batch file takes and helps finding errors.

Update after your second Code Update:

Try a structure like this:

[...]

goto eof
:Result
  ... your result block here
goto eof

[...]

:eof

With this you guard your :Result block from being executed when the interpreter just runs from top to bottom.
Also I would suggest not using ampersand chains (&). Prefer one command in each line if possible.
You missed to add all the % symbols. The code you posted just does not make any sense without them.
Also it seems that you deleted the :Result block entirely but you still call it...
I am confused.

Please check other sources for help, like for example www.ss64.com (CMD) and some tutorials maybe. I won't update my answer another time, sorry. This question was closed anyway.

Upvotes: 1

Related Questions