My goto forwarding does not work, but it echoes

@echo off  
:start1  
set /p input=action :   
for /f "tokens=1-2 delims= " %%a in ("%input%") do (  
goto :%%~a_%%~b >nul 2>&1 || goto start1
)    

If I put "| echo your input is not recognized", it works, but "goto start1" gives a script error message

:explore_room   
@echo room explored  
goto start1  
pause  
:examine_door  
@echo door examined  
pause  
:examine_wall  
@echo wall examined  
pause  
+4
source share
3 answers
@echo off  
:start1  
set /p input=action :   
call :%input: =_% 2>nul
if errorlevel 1 echo your input is not recognized
goto start1


:explore_room   
@echo room explored  
pause  
exit /B 0

:examine_door  
echo door examined  
pause  
exit /B 0

:examine_wall  
echo wall examined  
pause 
exit /B 0

Example:

action :   examine door
door examined
Presione una tecla para continuar . . .
action :   explore hall
your input is not recognized
action :   explore room
room explored
Presione una tecla para continuar . . .
¿Desea terminar el trabajo por lotes (S/N)? s
+3
source

A way to do this using the technique described here: Check if the label cmd @MC ND and @dbenham exist:

@echo off  
:start1  
set /p input=action :   
for /f "tokens=1-2 delims= " %%a in ("%input%") do (  
findstr /ri /c:"^ *:%%~a_%%~b " /c:"^ *::%%~a_%%~b$" "%~f0" >nul 2>nul && goto :%%~a_%%~b)
goto:start1


:explore_room   
@echo room explored
goto:start1
+2
source

!

, CALL GOTO.

goto :notExist || call :someLabel

,

.

, cmd- !

, goto .
a CALL, .

call :noLabel 2>nul || goto :thisWorks

, -, GOTO.
goto , .

|| .

, exit /b, , .

@echo off
setlocal DisableDelayedExpansion
set var=111
call :myFunc
set var
exit /b

:myFunc
setlocal EnableDelayedExpansion
set var=222
goto :noLabel 2>nul || set var=333!var!
echo This will be never reached
exit /b

= 333! !

, goto :noLabel exit /b, ENDLOCAL || set var=333!var!.

( ||) Dostips: GOTO CALL

+2
source

Source: https://habr.com/ru/post/1538298/


All Articles