my goto redirect is not working but works with echo

左心房为你撑大大i 提交于 2019-11-30 22:48:22
@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
SachaDee

A way to do that using the technics desribed here : Check if label exists cmd by @MC ND and @dbenham :

@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

This is a really strange and interessting bug!

The cause will be obvious when I used CALL instead of GOTO.

goto :notExist || call :someLabel

You get an error message like

Illegal to call a label outside of a batch file.

Obviously the parser switches to a cmd-line context here!

This is done when the first goto fails to find the label.
When you use first a call all works fine.

call :noLabel 2>nul || goto :thisWorks

This seems to be a general side effect of a failing goto.
When a goto fails, it normally stops immediatly the batch file.

But with the || operator the next command will be forced to execute.

But it seems that it works more like an exit /b, so you can use this effect to leave a function.

@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

The interessting output

var=333!var!

So the goto :noLabel acts like an exit /b and it also done the implicit ENDLOCAL before the part || set var=333!var! is executed.

The same issue (but without the || operator) was discussed at Dostips: Rules for label names vs GOTO and CALL

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!