DOS batch script which makes use of a call function to call another batch script

I need a DOS batch script which calls another batch script, but need conditions in the script to make sure:-

1) the script calls the other batch script successfully
2) the script exists if the script fails

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
@echo off
if exist OtherBatch.cmd  (
  call OtherBatch.cmd || echo This is an error
) else (
  echo Script not found

Open in new window

Steve KnightIT ConsultancyCommented:
Try something like this... is that what you mean?


@echo off
REM change to current directory of this batch file, mapping drive if needed for a UNC
pushd "%~dp0"
  if not exist 2.cmd (
    echo The batch file was not found, exiting
    exit /b

  call 2.cmd 2>NUL

  if errorlevel 1 (
    echo The other batch file was found OK and the error level was %errorlevel%
    exit /b
  ) ELSE (
    echo The other batch file was found OK and showed no error


Open in new window

and the second batch file then you can return the error code to the first with exit /b 1 etc., i.e.

exit /b 0 = OK
exit /b 1 = returns 1
exit /b 2 = returns 2
exit /b %errorlevel% = returns the errorlevel of the command before it

@echo off
echo Do your stuff here in batch 2
echo An error level returned on the exit statement as you need:

exit /b 1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
Crossed posts there with Qlemo of course... basically the same difference but I laid mine out a bit more wordily to show you other options.

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

rakkadAuthor Commented:
This task worked
Steve KnightIT ConsultancyCommented:
Why just a B grade?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Why only Steve got points (and why a "B" grade only)? Both solutions are comparable and without much of a difference.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.