Probably a dumb batch file question...

November 13, 2009 at 11:24:32
Specs: Windows XP
Hello, I am trying to do something that would seem simple enough here but I can't seem to figure it out.

I am trying to make a batch file that will check to see if a server is available. If it is then I need it to continue on, if not then check to see if another one is available and if that one is then do another task... something like this. (I know this isn't right its just the concept...)

ping -n 2 SERV1
if it is available goto:TASK1 otherwise goto:SERV2CHECK

:SERV2CHECK
ping -n 2 SERV2
if it is available goto:TASK2 otherwise stop the batch file

:TASK1
echo Doing stuff...

:TASK2
echo Doung other stuff...


See More: Probably a dumb batch file question...

Report •


#1
November 13, 2009 at 13:19:13
try:

setlocal enabledelayedexpansion
set srvr=SERV1
for /f "tokens=1* delims=" in ('ping -n 1 -w 1000 !srvr! | find /i "lost = 1"') do set test=%%a
if "!test!" equ "" goto :task1
set srvr=SERV2
for /f "tokens=1* delims=" in ('ping -n 1 -w 1000 !srvr! | find /i "lost = 1"') do set test=%%a
if "!test!" equ "" goto :task2
goto :xit
:task1
echo !srvr! available
:do more task1 stuff here
goto :xit
:task2
echo !srvr! is running
:do more task2 stuff here
:xit


might be bugs, i haven't tried it out. could be shortened by using a FOR /L loop since same code both servers.


Report •

#2
November 13, 2009 at 14:06:51
thanks for the help so far, I tried running this and got the error "in was unexpected at this time" and the window would immediately close... any ideas where the error may be?

Report •

#3
November 13, 2009 at 15:09:17
My bad, very sorry! (new to this myself)
foll. line is wrong:
for /f "tokens=1* delims=" in ('ping -n 1 -w 1000 !srvr! | find /i "lost = 1"') do set

in both lines, replace as:
for /f "tokens=1* delims=" %%a in ('ping -n 1 -w 1000 !srvr! | find /i "lost = 1"') do set


(i left out the %%a variable. apologies)


Report •

Related Solutions


Ask Question