I am using the following loop in a batch file to individually call 'functions' in the file starting by displaying text and finishing with an errorlevel check and display.
Calling the loop function uses 2 variables..
- the sub function name to be called
- the text to echo on screen for user ease
This is what those calling lines look like
call :loop BackupDatabase "Taking database backup"
call :loop StopBGService "Stopping the background service"
This is the loop function itself
:loop
:retry
echo %~2
call :%~1
IF %ERRORLEVEL% NEQ 0 (
SET ERRORLEV=%ERRORLEVEL%
ECHO Executing %~2 failed
ECHO Error Level: %ERRORLEV%
ECHO.
set /p ans=Do you want to retry %~2 [y/n]^>
if "%ans%"== "y" goto :retry
if "%ans%"== "Y" goto :retry
)
GOTO :EOF
Below you would find the functions that are called by each loop
:BackupDatabase
Perform database backup commands
GOTO :EOF
:StopBGService
Perform stop background service commands
GOTO :EOF
The problem I am having is when a "loop" goes wrong the script should ask if a retry is required, the very first time this happens, if you enter "n" it still retries? I've read over the loop script over and over and can't tell why this is. If anyone can fix this it would be a great help, or if someone can point out a better way of doing what I am trying to achieve.
choice
instead ofset
?