MegaRodeon
MegaRodeon

Reputation: 335

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

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.

Upvotes: 10

Views: 26001

Answers (4)

foxidrive
foxidrive

Reputation: 41224

Use Set variable=label1 or Set variable=main or Set variable=EOF before branching and use

goto :%variable%

to loop back or restart, or quit.

Upvotes: 1

Magoo
Magoo

Reputation: 79982

Unfortunately, you haven't said why you have a problem in the position you claim.

The solution

@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
echo Press any key to go from label2 to label1
pause >nul
goto label1

would seem far too obvious.

@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 call :label2

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

:label2
echo Reached label2 - Press any key to go from label2 to label1
pause >nul
goto :eof

where the colons :eof and call :label2 are required (meaning 'special label end-of-file understood by CMDandinternal subroutine` respectively.

However, some people ar under the impression that reaching a label terminates a batch routine in the same way as reaching the end of a paragraph or the } in C or end; in Pascal. It does not. Batch just charges on to the next sequential statement, like assembler.

I say this because your original code has no apparent way of terminating - on an entry of something other than 1 or 2 for instance, it would just run on through label1 and then through label2 - and if label2 goes back to label1, then you have an infinite loop.

One more item: If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes' If the user simply presses Enter then the if %choice% == 1 goto label1 would be interpreted as if == 1 goto label1 (since the variable choice would not be set) and this would generate a syntax error.

Oh - and choice isn't the best name for a variable (however logical and descriptive) since it's the name of a standard CMD utility.

From the prompt, try

choice /?

which might give you ideas about other methods of obtaining a er, choice...

Upvotes: 4

user330315
user330315

Reputation:

It's not totally clear to me what you want, but it sounds as if you are looking for a sub-routine which can be done using a call

@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

  goto :eof 

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

  goto :eof

:label2
  echo Reached label2
  goto :eof 

Upvotes: 0

Stephan
Stephan

Reputation: 56155

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

Upvotes: 25

Related Questions