Jul 28, 2012

Function: RetryCaller

You may find this useful if you want to continue calling some function X times till success and do not wish to copy-paste the retry routine. Below example can be freely modified; you can make it receiving fourth parameter that might be containing concrete ERRORLEVEL to stop retrying - the possibilities are endless.
:: works globally; uses vars: var0; cnt; temp_errorlevel
@echo on
set params="%~dpnx0" /force /noreboot
:: 3 params are needed: batch file label, number of iterations and NAME of the variable that contains parameters to be passed safely
call:retry testproc 10 params
:: will notify of success [0] or failure [1]
msg "%UserName%" "[%errorlevel%]"
set var0=%% %3 %%
set var0=%var0: =%
call:ThrowErrorLevel cnt 0
call call:%1 %var0%&&goto retry_loop_out
set/a cnt+=1
if not "%cnt%"=="%2" (goto retry_loop)
call:ThrowErrorLevel cnt 1
set temp_errorlevel=%errorlevel%
for %%A in (var0 %3) do (if defined %%A (set %%A=))
set temp_errorlevel=&exit/b%temp_errorlevel%
if defined %1 set %1=
:: demonstration of passing of parameters
echo %*
if "%cnt%"=="9" (exit/b0)
:: rem the above line to induce failure result
if "%cnt%"=="%cnt%" (exit/b1)

No comments:

Post a Comment