Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

my goto redirect is not working but works with echo

@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 the "goto start1" crashes the script

:explore_room   
@echo room explored  
goto start1  
pause  
:examine_door  
@echo door examined  
pause  
:examine_wall  
@echo wall examined  
pause  
like image 350
user3577444 Avatar asked Mar 20 '23 18:03

user3577444


2 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
like image 172
Aacini Avatar answered Apr 02 '23 02:04

Aacini


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
like image 20
SachaDee Avatar answered Apr 02 '23 01:04

SachaDee