retry command a set number of times

March 17, 2010 at 16:11:19
Specs: Windows 2003
I am looking to try and improve a batch file I wrote a while back. In the batch file I run a command if the command runs fine I exit out of the batch file if not I wait and retry the command again.

The way I retry the command is a little bit dirty I just run the command again from another label and then another label eg retry:, retry2: ,rety3: ect. I wanted to try and see if I could pass a command to the batch file eg Retry3 this would then try and run the program 3 times and would save me having to have so many retry labels. It would get very messy if I tried to run the program 99 times until it worked correctly


@ECHO OFF
set ERRORLEVEL=
TC %1 %2 %3 %4 %5 %6 %7 %8 %9
echo %ERRORLEVEL%
IF ERRORLEVEL 1 GOTO RETRY
IF ERRORLEVEL 0 GOTO END

:RETRY
ECHO.
ECHO %1 OF %2 FAILED LOOKING TO TRY AND %1 %2 AGAIN.
WAIT 20
set ERRORLEVEL=
TC %1 %2 %3 %4 %5 %6 %7 %8 %9
echo %ERRORLEVEL%
IF ERRORLEVEL 1 GOTO RETRY2
IF ERRORLEVEL 0 GOTO END

:RETRY2
ECHO.
ECHO %1 OF %2 FAILED ANOTHER TIME LOOKING TO TRY AND %1 %2 A SECOND TIME.
WAIT 20
set ERRORLEVEL=
TC %1 %2 %3 %4 %5 %6 %7 %8 %9
echo %ERRORLEVEL%
IF ERRORLEVEL 1 GOTO RETRY3
IF ERRORLEVEL 0 GOTO END


See More: retry command a set number of times

Report •


#1
March 17, 2010 at 18:24:14
@echo off & setLocal EnableDELAYedeXpansion
if %1'==' echo how many && goto :eof
set /a N=%1
:1
:: delay goes here
chkdsk z:
set /a N-=1 && if !N! equ 0 goto :eof
if errorlevel 1 goto :1


=====================================
Helping others achieve escape felicity

M2


Report •

#2
March 18, 2010 at 04:18:19
I got this, replaced WAIT with SLEEP to make it work here...

@ECHO Off

if "%1"=="" goto usage

set p_cnt=1
set p_cnt_max=%1
shift

:main
echo Run %p_cnt%
echo TC %1 %2 %3 %4 %5 %6 %7 %8 %9
IF ERRORLEVEL 1 GOTO RETRY
IF ERRORLEVEL 0 GOTO :EOF

:RETRY
ECHO.
ECHO %1 OF %2 FAILED LOOKING TO TRY AND %1 %2 AGAIN.
sleep 2
set /a p_cnt=%p_cnt% + 1
if %p_cnt% GTR %p_cnt_max% goto :EOF
goto main

:usage
echo.
echo USAGE - %0 repetition parameter(s)
goto :EOF


Report •

#3
March 18, 2010 at 09:26:01
Thanks Mechanix2Go for your always great answers in this forum.

Nice example as well tvc why did you use wait rather than sleep ? was it just preference ?


Report •

Related Solutions

#4
March 18, 2010 at 09:47:46
LOL nither wait nor sleep is part of windows.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
March 18, 2010 at 13:30:17
Yer I always have to use a 3rd party tool to do a wait I don't like the idea of the ping command that seems common to cause a pause.

have been looking for a wait command that shows wait 5,4,3,2,1

:-)


Report •

#6
March 18, 2010 at 15:25:58
Yeah, just to simulate a wait, cause WAIT I dont have, unlike SLEEP

You can also abuse the CHOICE command to simulate WAIT or SLEEP


Report •

#7
March 18, 2010 at 21:06:28
yer i had heard and see some one using the choice command :-)
i dont aways see the harm of using 3rd party tools as long as you deploy them with the application

Report •

#8
March 18, 2010 at 21:46:48
just found the command timeout is on win7 and 2003 not sure about xp

Report •

Ask Question