Batch file test connection before next step

Microsoft Windows xp professional w/serv...
December 22, 2010 at 03:59:23
Specs: Windows XP
I am trying to tidy a batch file up so that it will attempt the connection to the database before launching the webserver, if it fails it will attempt again up to 5 times but if connection is ok will move on to lauching webserver. I was hoping something i'd seen in another post might suffice but i'm completely new to batch files.

@echo & setLocal EnableDELAYedeXpansion
if %1'==' echo how many && goto :errormsg
set /a N=%1
:1
:: 30s
sqlplus user/pswd@localhost/XE
set /a N-=1 && if !N! equ 0 goto :startwebsvr
if errorlevel 1 goto :1

I don't want to go down creating 5 entries for each attempt i.e :retry1 :retry2 etc

Many thanks


See More: Batch file test connection before next step

Report •


#1
December 22, 2010 at 04:43:58
untested

=====================================
@echo off & setLocal EnableDELAYedeXpansion

set /p N=how many ?
for /L %%i in (1 1 !N!) do (
sqlplus user/pswd@localhost/XE
if not errorlevel 1 goto :start
)
goto :eof
:start
:: the work gets done here


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
December 22, 2010 at 05:52:53
Thanks for the great response Mechanix, i took out the user prompt and set it to repeat 5 times, tested on my machine as i knew it would create the error and works ok.
Would it be simple to add a delay inbetween the retries or would this need a bit code change?
Also, if i add this code into a larger batch file will i need to add endlocal?

@echo off & setLocal EnableDELAYedeXpansion
for /L %%i in (1 1 5) do (
sqlplus user/pswd@localhost/XE
if not errorlevel 1 goto :start
)
goto :errormsg

:start
pause (just for testing)
:errormsg
msgbox Application failed to load - Please call the helpdesk


Report •

#3
December 22, 2010 at 07:19:09
Think i've got it, used wait32.exe and works great, probably not the best way but working.

@echo off & setLocal EnableDELAYedeXpansion
for /L %%i in (1 1 5) do (wait32.exe /s30
sqlplus user/pswd@localhost/XE
if not errorlevel 1 goto :start
)
goto :errormsg


:start
pause

:errormsg
msgbox Application failed to load - Please call the helpdesk


Report •

Related Solutions

#4
December 22, 2010 at 09:09:51
With 30 sec delay CALLed as a sub. Don't leave out either goto :eof or it will lose the plot.

===========================================
@echo off & setLocal EnableDELAYedeXpansion

call :sub1
:main
echo other tasks here
goto :eof

:sub1
for /L %%i in (1 1 3) do (
sqlplus user/pswd@localhost/XE
if not errorlevel 1 goto :start
ping 1.1.1.1 -n 1 -w 30000 > nul
)
goto :eof
:start
:: the work gets done here
goto :eof


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
January 10, 2011 at 03:52:35
Hi Mechanix2go,

Thanks for the great info again, just a couple of questions on this.

Will the :eof in either of these close the batch file or just signal the end of that action? from what i've read i believe it ends the batch file but still a little unclear.

I want to call the errormsg batch file should all attempts at connecting to the database fail, is it possible to incorporate?

I should point out that in the example of what i was using above the full batch file did have EXIT at the end to close it once complete.


Report •

#6
January 10, 2011 at 04:16:08
just signal the end of that action


=====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question