Auto create batch script on failure

November 6, 2018 at 04:56:37
Specs: Windows 7
Hi All,

I am new to batch scripting, I wrote the below script to execute some of the jobs in sequence.

CALL Tna.bat JobTr -1 src_Load_1 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
ELSE
(CALL Tna.bat JobTr -1 src_Load_2 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
(CALL Tna.bat JobTr -1 src_Load_3 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
(CALL Tna.bat JobTr -1 src_Load_4 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
(CALL Tna.bat JobTr -1 src_Load_5 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)))))
exit /b 0

but when I execute the script only the first command is getting and rest of the commands are coming out.

I have googled and some of the people suggested to use CALL before the commands, I tried it but its still invoking the first command only.

Also, I need to create an ad-hoc script from the above, if the second command fails ,I need to generate a new .txt file with rest of the commands in the file like below , similarly if the third command return non zero return code, i need to generate a .txt file with the commands starting from the third command and rest of the commands


CALL Tna.bat JobTr -1 src_Load_2 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
(CALL Tna.bat JobTr -1 src_Load_3 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
(CALL Tna.bat JobTr -1 src_Load_4 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
)
(CALL Tna.bat JobTr -1 src_Load_5 -t seq1
IF %ERRORLEVEL% NEQ 0 (
exit /b 1
))))
exit /b 0

Please help to let me know if there are any guides or suggest some ideas on how to achieve this.

Thanks, Tara

message edited by taarini


See More: Auto create batch script on failure

Reply ↓  Report •

#1
November 6, 2018 at 07:06:46
Personally, I'd just have the script maintain a data file.
SET start=0
SET end=5
IF EXIST erroredOut <erroredOut SET /p start=
SET /A start=start

FOR /L %%a IN (%start%,1,%end%) DO CALL Tna.bat JobTr -1 src_Load_%%a -t seq1 || (>>erroredOut ECHO %%a& EXIT /B)

IF EXIST erroredOut DEL erroredOut

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#2
November 6, 2018 at 09:09:39
Thanks Razor for your quick response.

I tried executing the script, its calling all the commands ( 1-5), but if in between any job fails its still continuing to call all the commands instead of stopping and generating a file with rest of the commands.

How can we stop the script from executing the next script if the errorcode from the script is not equal to zero.

Thanks, Tara


Reply ↓  Report •

#3
November 6, 2018 at 11:40:32
Probably means erroredOut can't be created. That would mean the working directory is read-only. You should provide a full path to erroredOut. It should be a directory where the script has write access. Probably something like this, but not using temp. Temp can fail you in not immediately obvious ways. Including but not limited to, pointing to different locations, various disk cleanup activities that may happen between runs, and it will not play nice with any other scripts that pull the same trick.
SET start=1
SET end=5
SET file="%temp%\erroredOut"
IF EXIST %file% <%file% SET /p start=
SET /A start=start

FOR /L %%a IN (%start%,1,%end%) DO CALL Tna.bat JobTr -1 src_Load_%%a -t seq1 || (>%file% ECHO %%a& EXIT /B)

IF EXIST %file% DEL %file%

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

Related Solutions

#4
November 6, 2018 at 13:26:42
Thanks Razor, I have already mentioned the full path to the erroredOut and the file was getting created earlier.

This scripting is getting generated dynamically, but its not capturing the error code, if the %ERRORLEVEL% NEQ 0, it should not excute the next command and create a seperate file
with the failed command and the following commands

example, if the DO CALL Tna.bat JobTr -1 src_Load_3 -t seq1 failed a new.txt has to be created with

SET start=3
SET end=5
SET file="D:\Scripts\erroredOut.txt"
IF EXIST %file% <%file% SET /p start=
SET /A start=start

FOR /L %%a IN (%start%,1,%end%) DO CALL Tna.bat JobTr -1 src_Load_%%a -t seq1 || (>>%file% %%a& EXIT /B)

I know its giving my work to you, I will try to figure it out, will let you know if I need any logic help.

Thanks for your time.

Thanks, Tara

message edited by taarini


Reply ↓  Report •

#5
November 6, 2018 at 21:51:39
I'm really confused, as usual. I don't understand why your "base" script needs to write "downstream" scripts. Couldn't it all be handled in two scripts and, per Razor, recording the fail? Here is sample of simplified code:

xxx.bat - your "master" script. NOTE the 'setlocal enabledelayedexpansion' is usually required

@echo off & setlocal enabledelayedexpansion
for /l %%a in (1 1 5) do (
rem NOTE - IMPORTANT THAT TNA.BAT EXITS WITH ERRORLEVEL GTR ZERO on any fail
rem note that %errorlevel% doesn't work. you need the exclams etc. as !errorlevel!
rem EXCEPT when you use errorlevel as in :2
call tna.bat %%a
echo correct: --- !errorlevel!
echo useless: *** %errorlevel%
:2
rem any errorlevel greater than zero goes to "fail mode"
if errorlevel 1 goto :exit
)
goto :eof

:exit
echo failed at level: !errorlevel!
rem here is where you should record the last successful iteration into a file.
rem >> failed echo !errorlevel!
::========== end master sample-script

TNA.BAT sample script:

@echo off
:: this is just a simple test: if g1, g2 etc exist, then no error, else...
echo dir g%1
dir /b g%1 2>nul||(
rem tna exits with the errorlevel set to the failed iteration
exit /b %1
)
:: ---------- end code
To test, run in directory with some existing files: G1, G2 etc. The key is for TNA.BAT to exit with an errorlevel greater than zero when it hits a snag, and to use the "enable" clause at header of xxx.bat.


Reply ↓  Report •

Ask Question