0

How do you treat a user prompt as two words instead of one? for example,

set /p input=examine door          
set /p input=examine wall

would each take you to

:door
@echo door

:wall
@echo wall

I want a user to be able to type "examine" and then anything else and have it navigate to the correct label. I think there's some kind of wildcard I need to use but I'm not sure exactly how to do it.

Rafael
  • 3,042
  • 3
  • 20
  • 36

2 Answers2

0

May be something like this ?

@echo off

set /p input=action : 

for /f "tokens=1-2 delims= " %%a in ("%input%") do (
    goto :%%~a_%%~b >nul 2>&1 || echo no such action.
)

exit /b 0

:examine_door
@echo door examined
goto :eof

:examine_wall
@echo wall examined
goto :eof

EDIT - goto shows a strange bug when called with conditional execution and switches to command prompt context.So better CALL to be used. Pretty interesting bug and goto, setlocal and call does not work

@echo off

set /p input=action : 

for /f "tokens=1-2 delims= " %%a in ("%input%") do (
    call :%%~a_%%~b >nul 2>&1 || call :last_resort
)

exit /b 0

:examine_door
@echo door examined
goto :eof

:examine_wall
@echo wall examined
goto :eof

:last_resort
@echo nothing more can be done
goto :eof
Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • when I try to change the echo no such action to a goto, it doesn't work. How can I put in a different command here? – user3577444 Apr 27 '14 at 18:15
  • @user3577444 in all cases `goto` breaks the brackets context so you can use `call` instead.Check my edited code. – npocmaka Apr 27 '14 at 19:03
0

Here's an example of how you have take the text from the second token onward.

@echo off
set input=examine door
for /f "tokens=1,*" %%a in ("%input%") do echo goto :%%b
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68