Batch script with condition?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
prepek2000
Posts: 2
Joined: 01 Sep 2011 03:57

Batch script with condition?

#1 Post by prepek2000 » 01 Sep 2011 04:29

Hi guys!
I would like to create batch script for doing bunch of databases TNSPING.
I have created one batch file aca.bat and put in there this:

tnsping test1.bb.com
tnsping test2.bb.com
tnsping test1.bb.com
.
.
.
then i have execute it from cmd prompt like this aca.bat >>tnsping.log

and in tnsping.log file i have different output reply for each of above databases


D:\DATA\sta8983\Desktop>tnsping test1.bb.com

TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 01-SEP-2011 11:20:39

Copyright (c) 1997, 2003, Oracle. All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=test.bb.com)(Port=1639))(CONNECT_DATA=(SERVICE_NAME=test1.bb.com)))
OK (300 msec)

D:\DATA\sta8983\Desktop>tnsping test2.bb.com

TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 01-SEP-2011 11:20:39

Copyright (c) 1997, 2003, Oracle. All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=server.bb.com)(Port=1837))(CONNECT_DATA=(SERVICE_NAME=test2.bb.com)))
TNS-12541: TNS:no listener

D:\DATA\sta8983\Desktop>tnsping test3.bb.com

TNS Ping Utility for 32-bit Windows: Version 10.1.0.2.0 - Production on 01-SEP-2011 11:20:39

Copyright (c) 1997, 2003, Oracle. All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=manage.bb.com)(Port=1740))(CONNECT_DATA=(SID=test3.bb.com)))
TNS-12545: Connect failed because target host or object does not exist


So my question is it possible to create batch job which would going to put in log every dataabse which tnsping didnt finished with message OK at the end? I need to create that kind of list.

Thank you!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch script with condition?

#2 Post by aGerman » 01 Sep 2011 11:40

Does TNSPING return different %errorlevel% values depending on whether it succeeded or failed?

Regards
aGerman

prepek2000
Posts: 2
Joined: 01 Sep 2011 03:57

Re: Batch script with condition?

#3 Post by prepek2000 » 02 Sep 2011 00:23

Yes it does. I can expect that there is 4-5 diff values output of each pinged database. So therefore i would like to have log only from those which are not with OK output.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch script with condition?

#4 Post by aGerman » 02 Sep 2011 10:28

Maybe it makes sense to write each single output into a temporary log file, then check the errorlevel and append the temporary file to your list only if errorlevel is greater than 0.

Code: Select all

>"tmp.log" tnsping test1.bb.com
if errorlevel 1 >>"tnsping.log" type "tmp.log"

>"tmp.log" tnsping test2.bb.com
if errorlevel 1 >>"tnsping.log" type "tmp.log"

>"tmp.log" tnsping test1.bb.com
if errorlevel 1 >>"tnsping.log" type "tmp.log"

del "tmp.log"

Regards
aGerman

Post Reply