Solved

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

Posted on 2014-11-21
6
242 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 68

Expert Comment

by:Qlemo
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Closing Comment

by:rakkad
Comment Utility
This task worked
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Why just a B grade?
0
 
LVL 68

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now