0

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

Tea
  • 55
  • 3
  • 2
    possible duplicate of [Check if label exists cmd](http://stackoverflow.com/questions/21348579/check-if-label-exists-cmd) – MC ND May 24 '14 at 15:11

1 Answers1

2

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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This solution assumes that the chosen selection is one variable, the point of the loop is to separate the string into individual array variables. – Tea May 26 '14 at 09:33
  • Thanks Aacini that helped to get some error checking into my batch file, Greatly appreciate it. – Tea May 27 '14 at 10:59