Tea
Tea

Reputation: 55

Loop Variable checking

Just trying to check if a function exists before trying to execute that Function. I fear I may be trying to go to simple with this, I should probably be checking the string prior to executing the Process loop.

SET /P Selection=Please Select Options? 
echo You chose: %Selection% 

Call :Process

pause
goto :EOF

:Process
for %%A in (%Selection%) do (
if not exist :Opt%%A (Call :Redo) ELSE (Call :Opt%%A)
)
GOTO :EOF

:Redo
Echo %Selection%
SET /P Selection=Selection was Invalid, Please choose a Valid Option:
Call :Process
GOTO :EOF

:Opt1
ECHO Option 1's code
GOTO :EOF

So the problem I'm getting is that I seam to get stuck in if not exist statement and well is not allowing the functions to run correctly.

Thanks in advance

Upvotes: 0

Views: 46

Answers (1)

Aacini
Aacini

Reputation: 67196

When a CALL command is executed with a non-existent label an error message is displayed, but the process continue with an ERRORLEVEL = 1 after the call. You may made good use of this point in order to avoid to check if the label exists:

EDIT: Code modified as reply to the comment

SET /P Selection=Please Select Options? 
echo You chose: %Selection% 

:Process
for %%A in (%Selection%) do (
   Call :Opt%%A 2>NUL
   if errorlevel 1 (
      SET /P Selection=Selection was Invalid, Please choose a Valid Option:
      goto :Process
   )
)

pause
goto :EOF


:Opt1
ECHO Option 1's code
exit /B 0

Upvotes: 2

Related Questions