Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 errorlevel on DOS

Author  Topic 

etnad7
Starting Member

16 Posts

Posted - 2010-09-17 : 00:10:05
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

22859 Posts

Posted - 2010-09-17 : 01:39:12
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 - 2010-09-17 : 04:02:59
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

22859 Posts

Posted - 2010-09-17 : 09:35:29
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 - 2010-09-18 : 00:03:23
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

22859 Posts

Posted - 2010-09-18 : 02:29:08
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 - 2010-09-18 : 02:43:48
may I know what is ErrorExit: for? thanks!
Go to Top of Page

etnad7
Starting Member

16 Posts

Posted - 2010-09-18 : 02:49:42
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

22859 Posts

Posted - 2010-09-18 : 02:51:50
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:
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-18 : 02:54:09
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 - 2010-09-18 : 04:04:32
thank you so much Kristen!
Go to Top of Page
   

- Advertisement -