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:\libcopy textB.txt C:\libcopy textC.txt C:\libcopy textD.txt C:\libcopy textE.txt C:\libcorrect 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 errolevelof 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:\libIF ERRORLEVL 1 GOTO ErrorExitcopy textB.txt C:\lib...GOTO NoErrorErrorExit:... Error handler ...Or something like:copy textA.txt C:\libIF NOT ERRORLEVL 1 GOTO Step2Echo Error during copy of textA.txtGOTO ExitPointStep2:copy textB.txt C:\lib...ExitPoint:OrIF EXIST textA.txt GOTO Step1Echo File textA.txt not foundGOTO ExitPointStep1:copy textA.txt C:\lib... |
|
|
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%0C:\>copy textC.txt c:\libThe system cannot find the file specified.C:\>echo %errorlevel%1if 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! |
|
|
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 |
|
|
etnad7
Starting Member
16 Posts |
Posted - 2010-09-18 : 00:03:23
|
is it something like this? sorry just a newbie :)@echo oncopy textA.txt C:\libIF ERRORLEVEL 1 GOTO ErrorExitcopy textB.txt C:\libIF ERRORLEVEL 1 GOTO ErrorExitcopy textB.txt C:\libErrorExit:... Error handler ... |
|
|
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. |
|
|
etnad7
Starting Member
16 Posts |
Posted - 2010-09-18 : 02:43:48
|
may I know what is ErrorExit: for? thanks! |
|
|
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 ErrorExitC:\>copy textB.txt C:\lib 1 file(s) copied.C:\>IF ERRORLEVEL 1 GOTO ErrorExitC:\>copy textCXXX.txt C:\libThe 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. |
|
|
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 oncopy textA.txt C:\libIF ERRORLEVEL 1 GOTO ErrorExitcopy textB.txt C:\libIF ERRORLEVEL 1 GOTO ErrorExitcopy textC.txt C:\libIF ERRORLEVEL 1 GOTO ErrorExitGOTO NoErrorErrorExit:... Error handler ...NoError: |
|
|
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. |
|
|
etnad7
Starting Member
16 Posts |
Posted - 2010-09-18 : 04:04:32
|
thank you so much Kristen! |
|
|
|