Solved

Error handling in batch

Posted on 2008-10-06
8
656 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
  • 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 250 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vbscript 8 81
How to copu batch file to a network location 62 77
Run Internet Explorer as an administrator from CMD 20 102
Import filename in batch 2 25
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now