Solved

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

Posted on 2014-11-21
6
250 Views
Last Modified: 2014-12-06
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

Thanks
0
Comment
Question by:rakkad
  • 3
  • 2
6 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 40457392
@echo off
if exist OtherBatch.cmd  (
  call OtherBatch.cmd || echo This is an error
) else (
  echo Script not found
)

Open in new window

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40457393
Try something like this... is that what you mean?

Steve

@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
  )

popd

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

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40457399
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.

Steve
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Closing Comment

by:rakkad
ID: 40483246
This task worked
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40484092
Why just a B grade?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40484607
Why only Steve got points (and why a "B" grade only)? Both solutions are comparable and without much of a difference.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question