SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Other Forums
 Other Topics
 errorlevel on DOS
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

etnad7
Starting Member

16 Posts

Posted - 09/17/2010 :  00:10:05  Show Profile  Reply with Quote
hi guys, I have a batch script sample below.

copy textA.txt C:\lib
copy textB.txt C:\lib
copy textC.txt C:\lib
copy textD.txt C:\lib
copy textE.txt C:\lib

correct me if i'm wrong errorlevel captures only the last intructin which is the copy of textE.txt,
so even if textC does not exist, this batch will produce a errorlevel 0, how can i trace the errolevel
of all instructions capturing the error on 3rd intruction copy textC.txt? TIA!

Kristen
Test

United Kingdom
22415 Posts

Posted - 09/17/2010 :  01:39:12  Show Profile  Reply with Quote
I'm not sure you even get an ERRORLEVEL if file does not exist?

copy textA.txt C:\lib
IF ERRORLEVL 1 GOTO ErrorExit
copy textB.txt C:\lib
...
GOTO NoError


ErrorExit:

... Error handler ...

Or something like:


copy textA.txt C:\lib
IF NOT ERRORLEVL 1 GOTO Step2
Echo Error during copy of textA.txt
GOTO ExitPoint

Step2:
copy textB.txt C:\lib
...

ExitPoint:


Or


IF EXIST textA.txt GOTO Step1
Echo File textA.txt not found
GOTO ExitPoint

Step1:
copy textA.txt C:\lib
...
Go to Top of Page

etnad7
Starting Member

16 Posts

Posted - 09/17/2010 :  04:02:59  Show Profile  Reply with Quote
here's a sample.

C:\>copy textA.txt c:\lib
1 file(s) copied.

C:\>echo %errorlevel%
0

C:\>copy textC.txt c:\lib
The system cannot find the file specified.

C:\>echo %errorlevel%
1


if you noticed, when there's an error on copy of file, the code is 1, what i'm after is to fully check all those 5 copy instructions, for ex. all five we're copied successfully it will have a errorlevel code of 0 and if there's incomplete copy, it's errorlevel is 1. thanks!
Go to Top of Page

Kristen
Test

United Kingdom
22415 Posts

Posted - 09/17/2010 :  09:35:29  Show Profile  Reply with Quote
Well then I expect that either the First or Second example I gave will be suitable
Go to Top of Page

etnad7
Starting Member

16 Posts

Posted - 09/18/2010 :  00:03:23  Show Profile  Reply with Quote
is it something like this? sorry just a newbie :)

@echo on
copy textA.txt C:\lib
IF ERRORLEVEL 1 GOTO ErrorExit
copy textB.txt C:\lib
IF ERRORLEVEL 1 GOTO ErrorExit
copy textB.txt C:\lib

ErrorExit:

... Error handler ...
Go to Top of Page

Kristen
Test

United Kingdom
22415 Posts

Posted - 09/18/2010 :  02:29:08  Show Profile  Reply with Quote
Yup, that should do it. Test by manually adding XXXXXX to the filename for textA, check it errors out, then repeat for textB file and so on.
Go to Top of Page

etnad7
Starting Member

16 Posts

Posted - 09/18/2010 :  02:43:48  Show Profile  Reply with Quote
may I know what is ErrorExit: for? thanks!
Go to Top of Page

etnad7
Starting Member

16 Posts

Posted - 09/18/2010 :  02:49:42  Show Profile  Reply with Quote
pls. see what happened, i tried to forced error on textC. thanks!

C:\>copy textA.txt C:\lib
1 file(s) copied.

C:\>IF ERRORLEVEL 1 GOTO ErrorExit

C:\>copy textB.txt C:\lib
1 file(s) copied.

C:\>IF ERRORLEVEL 1 GOTO ErrorExit

C:\>copy textCXXX.txt C:\lib
The system cannot find the file specified.

C:\>ErrorExit:
'ErrorExit:' is not recognized as an internal or external command,
operable program or batch file.

C:\>... Error handler ...
'...' is not recognized as an internal or external command,
operable program or batch file.
Go to Top of Page

Kristen
Test

United Kingdom
22415 Posts

Posted - 09/18/2010 :  02:51:50  Show Profile  Reply with Quote
The program flow will jump to ErrorExit if there is an error. This will prevent attempting to copy any further files (but any files already copied will NOT be reversed).

At the ErrorExit point you could add additional error handling. For example, you could create a dummy file to indicate that the process had terminated with error, or you could append an Error Message to a log file ...

Make sure that the normal execution of the batch file does not fall-through into ErrorExit:

@echo on
copy textA.txt C:\lib
IF ERRORLEVEL 1 GOTO ErrorExit
copy textB.txt C:\lib
IF ERRORLEVEL 1 GOTO ErrorExit
copy textC.txt C:\lib
IF ERRORLEVEL 1 GOTO ErrorExit

GOTO NoError

ErrorExit:

... Error handler ...

NoError:

Edited by - Kristen on 09/18/2010 02:52:14
Go to Top of Page

Kristen
Test

United Kingdom
22415 Posts

Posted - 09/18/2010 :  02:54:09  Show Profile  Reply with Quote
Sorry, a Label in a batch file should START with a ":" - in all other languages I have ever used Labels END with a Colon.
Go to Top of Page

etnad7
Starting Member

16 Posts

Posted - 09/18/2010 :  04:04:32  Show Profile  Reply with Quote
thank you so much Kristen!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000