Batch goto with condition

batch goto loop
batch file goto statement
batch goto not working
batch if goto
batch goto return
batch goto :eof
batch echo and goto
multiple goto in batch file

I wanted to call the specific subroutine for which the parameter is true. If both the parameter are false then exit. I have tried different stuff but I cannot find a solution for my problem.

I have following scenario:

echo OFF
set APP=TRUE
set BPP=TRUE

IF /i "%APP%"=="true" goto sub1
IF /i "%APP%"=="true" goto sub2

echo Both are set false
goto CLOSE

:sub1
echo This is sub1

:sub2
echo This is sub2

:CLOSE
echo Nothing is selected 
exit /B 1

The scenario is like the following : If ONLY APP is true I want just the sub1 to executed, if ONLY BPP is true then I want only the sub2 to be executen. If APP and BPP are both true then sub1 has to be executed first and the sub2 has to be executed. However if APP and BPP both are set false then the CLOSE has to be executed.

Thanks in advance.

You could simply add another conditional in :sub1:

@echo OFF
set "APP=TRUE"
set "BPP=TRUE"

if /I "%APP%"=="true" goto :sub1
if /I "%BPP%"=="true" goto :sub2

echo Both are set to FALSE
goto :CLOSE

:sub1
echo This is :sub1
if /I not "%BPP%"=="true" goto :end

:sub2
echo This is :sub2
goto :end

:CLOSE
echo Nothing is selected
exit /B 1

:end
echo At least one is set to TRUE

Or you could just invert your if queries and skip over the code portions conditionally; to detect whether both code sections :sub1 and :sub2 were skipped you could use a flag-like variable:

@echo OFF
set "APP=TRUE"
set "BPP=TRUE"
set "FLAG="

if /I not "%APP%"=="true" goto :sub2
:sub1
echo This is :sub1
set "FLAG=#"

if /I not "%BPP%"=="true" goto :skip
:sub2
echo This is :sub2
set "FLAG=#"

:skip
if defined FLAG goto :end
echo Both are set to FALSE

:CLOSE
echo Nothing is selected
exit /B 1

:end
echo At least one is set to TRUE

You could also replace goto by call and do it like this, also using a flag-style variable:

@echo OFF
set "APP=TRUE"
set "BPP=TRUE"
set "FLAG="

if /I "%APP%"=="true" call :sub1
if /I "%BPP%"=="true" call :sub2

if defined FLAG goto :end
echo Both are set to FALSE
goto :CLOSE

:sub1
echo This is :sub1
set "FLAG=#"
goto :EOF

:sub2
echo This is :sub2
set "FLAG=#"
goto :EOF

:CLOSE
echo Nothing is selected
exit /B 1

:end
echo At least one is set to TRUE

Goto - Jump to label - Windows CMD, GOTO. Direct a batch program to jump to a labelled line. Syntax GOTO label a negative conditional execution, the line containing the GOTO will be executed,  GOTO can only be used in batch files. After a GOTO command in a batch file, the next line to be executed will be the one immediately following the label. The label must begin with a colon [:] and appear on a line by itself, and cannot be included in a command group. The colon is required on the line where the label is defined, but is not required in the GOTO command itself. Case differences are ignored when matching labels.

use call (which returns) instead of goto. Also you forgot to end your subroutines, so the code would `run through":

echo OFF
set APP=TRUE
set BPP=TRUE

if /i "%APP%%BPP%"=="falsefalse" (
  echo Both are set false
  echo Nothing is selected 
  exit /B 1
)   
IF /i "%APP%"=="true" call :sub1
IF /i "%BPP%"=="true" call :sub2
echo done.
exit /b 0

:sub1
echo This is sub1
goto :eof

:sub2
echo This is sub2
goto :eof

goto, Within a batch program, goto directs command processing to a line You can use goto with other commands to perform conditional operations. If your batch program does not contain the label that you specify in Label, the batch program stops and displays the following message: Label not found Using goto for conditional operations. You can use goto with other commands to perform conditional operations. For more information about using goto for conditional operations, see the If command reference.

This should do it:

echo OFF
set APP=TRUE
set BPP=TRUE

IF /i "%APP%"=="true" goto sub1
IF /i "%BPP%"=="true" goto sub2

echo Both are set false
goto CLOSE

:sub1
echo This is sub1
IF /i "%BPP%"=="false" goto CLOSE

:sub2
echo This is sub2

:CLOSE
echo Nothing is selected 
exit /B 1

Note the addition of the conditional at the end of sub1.

GOTO - Branch to a specified line in a batch file, Using /I under any other conditions will cause an error later in your batch file. You cannot branch into another IFF statement, another DO loop, or a different IFF or  Batch File Goto GOTO is a command used to branch from a particular point in the program unconditionally. GOTO statements also allow us to simulate the loops without the use of for statements in the program.

Batch files, In "real DOS", the GOTO command is used to skip part of a batch file: @ECHO OFF • • CHOICE /C:123 /N Choose 1, 2 or 3 IF ERRORLEVEL 3  In NT, a call to a subroutine's label (CALL :label), combined with GOTO :EOF to terminate the subroutine, presents a way to create real subroutines, GOTO :EOF being the instruction to jump back to where the subroutine was called.

Batch File Operators, If Else, Goto and For Loop, Conditional statements allow us to do different things in our program based on the conditions. Without these conditional statements, our code would do the same  or GOTO:MySubroutine (a colon in place of the space) EOF. The eof label is a special case - using GOTO:eof will always transfer execution to the end of the current batch file or the end of the current subroutine. This can be written as GOTO:eof or GOTO :eof the space is optional. GOTO EOF and GOTO :EOF are not the same.

More Powerful Batch Files- Branching with "If" statements, More Powerful Batch Files Part I - Branching and Looping. By Vic Laurie For example you might have a command of the type: if (condition) goto :label  The general working of this statement is that first a condition is evaluated in the ‘if’ statement. If the condition is true, it then executes the statements thereafter and stops before the else condition and exits out of the loop. If the condition is false, it then executes the statements in the else statement block and then exits the loop.

Comments
  • This is simple programming logic. Programming is at its base is simple logic. So you would do your tests in the order that would make it easy. So IF /i "%APP%"=="true" IF /i "%BPP%"=="true" goto Whicheversubforboth and do your tests in correct order.
  • The second if /I "%APP%"=="true" should actually read if /I "%BPP%"=="true", correct?
  • No, that is the first of 4 lines.