Nepal12
Nepal12

Reputation: 593

Batch goto with condition

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.

Upvotes: 0

Views: 112

Answers (3)

Stephan
Stephan

Reputation: 56155

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

Upvotes: 5

aschipfl
aschipfl

Reputation: 34899

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

Upvotes: 2

steliosbl
steliosbl

Reputation: 8921

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.

Upvotes: 1

Related Questions