Solved

windows batch file error handling

Posted on 2008-11-02
19
2,498 Views
Last Modified: 2008-11-03
Can someone give me an example and explanation of how to write error handling capabilities in a windows batch file?
0
Comment
Question by:dchau12
  • 7
  • 5
  • 5
  • +1
19 Comments
 
LVL 4

Expert Comment

by:Kaddict
ID: 22862085
Here's a couple links about ERRORLEVEL which will help you handle error codes :

http://www.robvanderwoude.com/errorlevel.html
http://www.allenware.com/icsw/icsw020.htm
http://kennethhunt.com/archives/000933.html (search for errorlevel you'll find another example how to use it)

0
 

Author Comment

by:dchau12
ID: 22862137
thats nice...

I am trying this code but it do what I want it to.  the error level doesn't change even if an error occurs:

echo %errorlevel%
cd c:\doesnetexist
echo %errorlevel%

What I would really like is the following:
if %errorlevel%>0 goto ERR

commands.....

:ERR
echo error has occured
Exit



0
 
LVL 69

Expert Comment

by:Qlemo
ID: 22862148
If you are only interested in success/fail, it's often better to use command chaining:

(echo x | findstr x >nul) && echo found it
(echo x | findstr y >nul) || echo not found
 
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 69

Expert Comment

by:Qlemo
ID: 22862156
Which Windows version?
0
 
LVL 4

Expert Comment

by:Kaddict
ID: 22862158
Your error is that you can't use  " > " because in dos this is the REDIRECT command

paste my code into a new test.cmd file and you'll see that it works
To run it go into dos and type test to run the script where you saved it

-kaddict

@echo off
cls
echo %errorlevel%
cd c:\doesnetexist
echo %errorlevel%
if %errorlevel% GTR 0 goto ERR
 
echo Should not see this!
 
:ERR
echo Error has occured!!!!!

Open in new window

0
 
LVL 4

Expert Comment

by:Kaddict
ID: 22862162
Info about GTR and more of his friends (
EQU - equal
     NEQ - not equal
     LSS - less than
     LEQ - less than or equal
     GTR - greater than
     GEQ - greater than or equal
)

can be found there : http://www.computerhope.com/if.htm
0
 

Author Comment

by:dchau12
ID: 22862166
I dont think that code is correct Kaddict.  The error level started at 1 and after the error was still 1.  So, the error level never changed.
0
 

Author Comment

by:dchau12
ID: 22862170
The window version is xp pro version 2002 service pack 2
0
 
LVL 4

Expert Comment

by:Kaddict
ID: 22862172
true. we're missing one more little thing here :)
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 22862175
I for myself would prefer this:


cd c:\doesnetexist || goto ERR
 
echo Never have to see this
goto :EOF
 
:ERR
echo Error!

Open in new window

0
 

Author Comment

by:dchau12
ID: 22862183
um... that worked.

Can you please explain a bit more?  will this work for every error?
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 22862193
Yes, it works for all errors reflecting in a changed errorlevel. But you can not differentiate for the error code, besides you store the stderr output in a file and process it later.
Example:

cd c:\doesnotexist 2>cd.error || (echo Error occurred, the detail is | type cd.error)

0
 
LVL 4

Expert Comment

by:Kaddict
ID: 22862199
If you ever still want it to work the ERRORLEVEL way, here's something that works:

I downloaded the script ERRORLEVEL.bat from http://www.robvanderwoude.com/files/errlevel.zip and extracted it on my D:\

Then on my D:\> prompt I typed :

cd \
errorlevel
(the script returns ERRORLEVEL 0)
cd \doesnotexist
errorlevel
(the script returns ERRORLEVEL 1)
cd \
errorlevel
(the script returns back again ERRORLEVEL 0 so it's working)

But... the script ain't a line or two, its complicated and working up to 255 error levels so I would recommend using it if you want an ERRORLEVEL solution, but explaining it exactly wouldn't be easy =)

good luck,

-kaddict
0
 
LVL 4

Expert Comment

by:Kaddict
ID: 22862211
Of course I agree that Qlemo's looks way better than using ERRORLEVEL which is old and not simple to use.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 22862244
However, little typo in my example:

cd c:\doesnotexist 2>cd.error || (echo Error occurred, the detail is & type cd.error)
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 22862733
I don't agree with the selected solution on this.  While errorlevel can do what you want, there's an easier way when it comes to checking for files and folders -

If NOT EXIST C:\DOESNOTEXIST GOTO ERRORS
0
 

Author Comment

by:dchau12
ID: 22867171
Define easier, because Qlemo's way works just fine.
0
 
LVL 4

Expert Comment

by:Kaddict
ID: 22872330
It is true that leew's solutions applies more to the specific problem, and by easier I guess he mean that it's easier to understand, but the already-accepted solution works and applies to more conditions that file/directory existing...

(and here goes the debate xD)

Nice day too all

-kaddict
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 22872430
Easier - Qlemo's way is a multistep process:

first do something, then check for the error.

Using If Not Exist (or If Exist) allows for the entire process in one line.

For example, lets say I want to copy a file to the folder c:\myfolder.

Using Errorlevel:

cd /d c:\myfolder
if %errorlevel% == 1 md c:\myfolder
cd /d c:\myfolder
copy c:\myfile.txt c:\myfolder

Using If [NOT] Exist:

If not exist c:\myfolder md c:\myfolder
copy c:\myfile.txt c:\myfolder


As I said, using ErrorLevel can do what you want, but it's not the most appropriate solution to non-existent files/folders.  If you want to learn batch programming, you need to be aware of when to use what methods.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to App source 6 75
Win 10...Some user folders missing location tab 7 84
full control root but more restrictive lower permissions 1 38
List out file 4 27
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

821 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