Looping in Windows batch script

No Comments

We have a Rational Team Concert process that is executed by a scheduled task. The task tries to download the code from a server that unfortunately does act up from time to time during the middle of the download process. RTC currently does not have a retry function so the result needs to be manually checked and the process be restarted. Its not convenient so I created a batch loop in order to minimize the human interaction.

Below is the base script that will retry the operation up to the figure specified in the attempts variable. The script uses the ping command to effect a delay between the retry attempts. For what it is worht I am releasing it under Creative Commons just for the sake of attribution. 🙂


REM
REM Function: A sample batch script on how to retry a utility or another batch script that fails intermittently.
REM
REM Author: ramfree17
REM Email: ramfree17@gmail.com
REM
REM License under: Creative Commons Attribution v3.0 Unported [http://creativecommons.org/licenses/by/3.0/]
REM

@echo off

set attempts=10
set counter=0

call :load_func

goto done

:load_func
set /a counter+=1
echo Attempt %counter% out of %attempts%

if %counter% GTR %attempts% goto :eof

C:\ibm\sdp\scmtools\eclipse\scm load "TestWS" -r "https://someserver.com/jazz" -u "testUser" -d "d:\Workspace\rtc" -f -P [password]

REM Check the result. 0 is successful.
if %ERRORLEVEL% NEQ 0 ( ping -n 10 google.com
call :load_func)

goto :eof

:error_section
echo. ERROR: Unable to perform the action. Please see the log if any.

:done
echo exiting sample script.

Leave a Reply