Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return to previous label in batch ('goto' command)

Tags:

batch-file

For example,

@echo off
goto main

:main
echo Select:
echo 1) Goto label 1
echo 2) Goto label 2
set /p choice=
if %choice% == 1 goto label1
if %choice% == 2 goto label2

:label1
echo Will now direct you to label2
echo Press any key to go to label2
pause >nul

:label2 [PROBLEM HERE]
echo Type 'N' or 'E' and press Enter to go back to label1 or exit.
set /p choice2=
if %choice2% == N goto label1
if %choice2% == E exit

Please ignore the part where it says 'goto main', I know it isn't necessary but I've got used to it.

The "PROBLEM HERE" indicates the part where I want the batch to return to label1, without actually typing the code to specify to go back to previous label, as sometimes I might need label1 to work something to go to label2 and work another thing, then go back to label1 again to continue its business.

like image 610
MegaRodeon Avatar asked Jan 12 '14 12:01

MegaRodeon


People also ask

What does goto do in batch?

In a nutshell, the goto command is a way to control the flow of a batch file. Typically, when you execute a batch file, the script executes from top to bottom following each line. But sometimes, you need the script to start executing at a different place in the script. The goto command is perfect for this.

What does the goto command do?

The goto statement can be used to alter the normal flow of control in a program. This statement causes the program to jump to a specified label.

What does %% A mean in batch?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.


1 Answers

goto has no "Return". But you can call a part of your script:

@echo off
echo main program
call :label1
echo main program
call :label2
echo main program

pause
exit /b

:label1
echo subroutine
goto :eof

echo never reached

:label2
echo sub two
goto :eof
like image 73
Stephan Avatar answered Oct 19 '22 16:10

Stephan