Only fatal errors (command not found) will stop the batch file. If a program returns an error, the batch execution is continued nevertheless.
Main Topics
Browse All TopicsHi
I would like my MSDOS script to continue running, ie executing the next line
despite encountering error :
Script.bat :
program1.exe parameters 2>> c:\temp\err1.txt
program2.exe parameters 2>> c:\temp\err2.txt
.....
programX.exe parameters 2>> c:\temp\err3.txt
What's the line to insert at the beginning of the script such that it does not
stop/exit upon encountering error conditions
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
is it possible to force the errorlevel to be always 0 throughout the script :
set errorlevel=0
call program1.exe parameters 2>> c:\temp\err1.txt
call program2.exe parameters 2>> c:\temp\err2.txt
.....
call programX.exe parameters 2>> c:\temp\err3.txt
OR
call program1.exe parameters 2>> c:\temp\err1.txt
IF %ERRORLEVEL NEQ 0 goto pgm2
:pgm2
call program2.exe parameters 2>> c:\temp\err2.txt
IF %ERRORLEVEL NEQ 0 goto pgm3
.....
IF %ERRORLEVEL NEQ 0 goto pgmX
:pgmX
call programX.exe parameters 2>> c:\temp\err3.txt
The %errorlevel% is a code issued after the last executed command finishes, and so setting it to Zero at the start of the batch file will have no effect.
Your 2nd alternative uses a pretty standard check of the return code and should work if you remember to add on the closing % on %ERRORLEVEL
One trick you can may find useful if CALLing other batch files is to place an EXIT /b 0 command on the last line of the CALLed batch file. The /b switch exits the batch file but does not exit cmd.exe, so by adding the Error Code Zero as the exit code, control is passed back to the calling batch file where it left off with an %errorlevel% of Zero.
You can use the EXIT /b <code> command within the same batch file to allow a graceful exit from a looping bit of code while specifying an exit code.
Did you check to see if your programs have a "continue even if errors" command line switch?
What are the programs?
Perhaps there may be a better workaround than your 2nd usage example above.
Have you considered this as an option?
@echo off
cmd /c NLS_Archive.exe 2>err
for %a in (err) do (
if %%~za gtr 0 (
echo An error occured running: NLS_Archive.exe
) else (
echo NLS_Archive.exe successfully completed
)
)
cmd /c st_to_Msar_convert.exe 2>err
for %a in (err) do (
if %%~za gtr 0 (
echo An error occured running: st_to_Msar_convert.exe
) else (
echo st_to_Msar_convert.exe successfully completed
)
)
Re-posted due to missing '%' sign....
Have you considered this as an option?
@echo off
cmd /c NLS_Archive.exe 2>err
for %%a in (err) do (
if %%~za gtr 0 (
echo An error occured running: NLS_Archive.exe
) else (
echo NLS_Archive.exe successfully completed
)
)
cmd /c st_to_Msar_convert.exe 2>err
for %%a in (err) do (
if %%~za gtr 0 (
echo An error occured running: st_to_Msar_convert.exe
) else (
echo st_to_Msar_convert.exe successfully completed
)
)
Or how about something like this:
@echo off
call :execute NLS_Archive.exe /a /b /c /other_options /etc...
if not %errorlevel%==0 echo error or do something else
call :execute st_to_Msar_convert.exe /more_options /etc...
if not %errorlevel%==0 echo error or do something else
call :execute dir /b
if not %errorlevel%==0 echo error or do something else
::
exit /b
:execute
%* 2>err
for %%a in (err) do exit /b %%~za
Sorry, but if I do a
x & y
both "commands" are tried to be called. Two error messages. The batch file continues, I do not need to check for existence or something, as long as I do not care for the error messages.
About that "command not found" will stop the batch file, I stand corrected, as we can see, and t0t0 stated correctly. A fatal error is a batch syntax error only.
Business Accounts
Answer for Membership
by: OvePosted on 2009-11-07 at 04:18:12ID: 25765987
prefix the execution with "call"
Script.bat :
call program1.exe parameters 2>> c:\temp\err1.txt
call program2.exe parameters 2>> c:\temp\err2.txt
.....
call programX.exe parameters 2>> c:\temp\err3.txt