Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error handling in batch

Posted on 2008-10-06
8
Medium Priority
?
674 Views
Last Modified: 2008-10-07
I am learning how to add some error handling to my batch files.  Could someone please tell me why this does not work?
I always returns that it worked, even if I put bad entries in the banner_servers file.


For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	shutdown -f -r -m \\%%N -t 00 >> shutdownresults.txt) & (
	if errorlevel 0 echo "%%N successfully sent the reboot command" >>shutdownresults.txt) & (
	ping -n 1 -w 5000 x.x.x.x)

Open in new window

0
Comment
Question by:loftyworm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 22653042
IF ERRORLEVEL works differently than other IF statements.  if errorlevel 0 will be true if the errorlevel is equal to or greater than zero.  This means that it will always be true.

You probably want if %errorlevel% GTR 0 instead.

This page has more information:
http://www.ss64.com/nt/if.html
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 1000 total points
ID: 22653045
Correction.  You want if %errorlevel%==0.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22653049
Don't use Errorlevel 0. Errorlevel 0 is always set. I haven't found out why yet. My guess is Errorlevel 0 is set if the previous command was run. Evaluate errorlevel 1.

In your case change if errorlevel 0 to if not errorlevel 1.

Errorlevel 1 is set if the command failed.
0
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 
LVL 11

Author Comment

by:loftyworm
ID: 22653198
:(  
I tried both ways (and added a little else statement) but it is still saying it always works


For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt) & (
	shutdown -f -r -m \\%%N -t 00 >> shutdownresults.txt) & (
        if %errorlevel%==0 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%"))& (
rem 	if not %errorlevel%==1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%"))& (
	ping -n 1 -w 5000 x.x.x.x)

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22653272
if not errorlevel 1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%"))& (
      ping -n 1 -w 5000 x.x.x.x)
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 1000 total points
ID: 22653356
Pretty messy the way you have it.  Also I don't think shutdown is setting the errorlevels when it cannot contact the workstation.
For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	shutdown -f -r -m \\%%N -t 00 >> shutdownresults.txt
 	if not errorlevel 1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%")
	ping -n 1 -w 5000 127.0.0.1
)

Open in new window

0
 
LVL 11

Author Comment

by:loftyworm
ID: 22653689
So its not me????
Its the shutdown.exe that is not setting the errorlevel?
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22664194
Try this.
For /f %%N in (Banner_servers.txt) DO (
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	echo -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- %%N >> shutdownresults.txt
	shutdown -f -r -m \\%%N -t 00 | FIND /i "The network path was not found."
 	if errorlevel 1 (echo "%%N Successfully sent the reboot command") else (echo "%%N returned an error %errorlevel%")
	ping -n 1 -w 5000 127.0.0.1
)

Open in new window

0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question