I have a batch file that calls various commands, some of which will occasionally fail due to network issues. Re-trying the command will usually result in success.
How can I re-try the commands automatically, up to a set number of tries?
Here is the some pseudo code that aims to explain further
call:try numTries "command and arguments"
exit
:try
REM execute %2, trying upto %1 times if it fails
%1 = %1 -1
eval %2
if %errorlevel%==0 exit \B
if %1 > 0 goto try
exit \B
The following script be be what you are looking for:
The logic of the
trysub-routine is this:Store the number of tries into a variable.
Begin the loop. Check the
triesvariable. If 0 or less, return.Evaluate the command and arguments.
If the returned value is ‘success’ (ERRORLEVEL is 0), return (from the
tryroutine), otherwise go to #2 (the beginning of the loop).