Solved

Error handling in batch

Posted on 2008-10-06
8
650 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
The viewer will learn how to count occurrences of each item in an array.
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now