Direct invoking vs call vs start - windows

I'm having this issue when running a batch file with Tivoli Workload Scheduler.
There's a third party program, let's says its name is program.exe
The batch file contains the following command to invoke program.exe
program.exe param1 param2 param3
The problem is the batch file terminates when there are warning popups from program.exe; but we're totally ok with the warning popups. We want it to run regardless of how many warnings it encounters.
I've looked into this and found out that using 'start' could solve the problem.
'call' behaves the same way as direct invoking.
So when we directly invoke the program does it default to ?
call program.exe
or is there any significant difference between direct invoke and call ?

is there any significant difference between direct invoke and call ?
No difference: you would call to call another cmd batch script.
It also ensures you return to the current script once the call is done.
You can also use it to call an function within your current script.

For the program it's not a difference, but you got different results for the parameters, as the parameters will be evaluated two times by the parser.
program Program^&Documents "One caret ^ "
call program.exe Program^&Documents "One caret ^ "
The first line works as expected, but the second results into
program.exe Program
&Documents "One caret ^^ "
And it fails completly because the & can't be avaluated in a CALL.
And carets are doubled by a call.

Related

Windows cmd START batch file in new console with quoted arguments

Note: A solution is posted below.
Problem:
When required to start a batch file (cmd script) (i.e. CALLEE.CMD) in a new console window, from within another batch file (i.e. CALLER.CMD) , the obvious choice would be to use the internal START command: START "" "%~DP0CALLEE.CMD".
However, if we need to pass quoted parameters to CALLEE.CMD, such as START "" "%~DP0CALLEE.CMD" "ARGUMENT_1", you get an error such as 'CALLEE.CMD" "ARGUMENT_1' is not recognized as an internal or external command,
operable program or batch file.
At first glance, it's not obvious why this command should fail, as the requirements for calling the START command seem to be met. This question is posted because the reason for failure is not immediately apparent, and others may find the reference useful.
So, why is this command failing, and how to resolve the issue?
What is happening here, is that when the START command detects the command to be invoked is either an internal command (internal to cmd.exe) or a batch file, it will invoke cmd.exe to handle the command (cmd.exe /K ... will be used). The additional arguments passed to START will be translated and passed to cmd.exe. When this happens, the rules change: cmd.exe expects the command and arguments to be quoted differently from START, so this has to be taken into account when writing the original command.
The quoting for cmd.exe is documented elsewhere, such as:
correct quoting for cmd.exe for multiple arguments
http://ss64.com/nt/cmd.html.
So, to start a batch file, with quoted arguments in a new console window, we could do as follows:
START cmd.exe /K ""%~DP0CALLEE.CMD" "ARGUMENT_1" "ARGUMENT_2""
Of course, this problem only occurs if you need to quote the arguments, either because a called program expects it or if there are spaces in the argument.
Also, as a plus, if you don't require the new console window to remain open after finishing the batch file, you can substitute the switch /K with /C.
The requirement for always needing to provide a window title when calling a quoted command is avoided here, but if you do require to prefix the title of the new console window, you can do as follows:
START "<Title>" cmd.exe /K ""%~DP0CALLEE.CMD" "ARGUMENT_1" "ARGUMENT_2""
Example code:
CALLEE.CMD
#ECHO %*
#PAUSE
CALLER.CMD
#START "<Title>" cmd.exe /C ""%~DP0CALLEE.CMD" "ARGUMENT_1" ARGUMENT_2 "ARGUMENT_3""

Calling two functions in Windows batch file one after the other

I have two scripts which are to be executed and i am using Windows batch script for automating of running the scripts.
I have to read 10 user input values and then run the two scripts using those parameters.
I am successful in executing the first script and failing with the second script.
Issue is that the cmd prompt exits after completing the first script.
How to make cmd prompt run the second script as well using the input parameters.
Any Help on this??
Thanks in Advance.
You have two options to execute a batch script from you code. It's either START or CALL:
START will execute your code in it's own variable scope, means the variables you've set in the first script won't be available. Further both scripts will be executed in parallel and not one after another (unless you use START /WAIT).
CALL on the other hand will do exactly what you need. It will start the first script in the same scope (previously set variables are variable), execute it and afterwards it will run the second sript (also in the same scope).
TL;DR this will work:
...
CALL BatchScript1.bat
CALL BatchScript2.bat
...
If you call scripts from another script you have to use the call command:
#echo off
call first.cmd
call second.bat
echo Here I'm back again !

Call to .cmd file causes exit from parent .cmd file

I have a .cmd file which calls another .cmd file, as follows
parent.cmd
call "C:\Program Files\Prog1\bin\dostuff.cmd" -abc="def"
After def.cmd has run, the dos window skips to the next line, showing the prompt >
The parent.cmd file has therefore completed execution, according to the command prompt. However, after the call to dostuff.cmd, the parent.cmd file is not complete and has a number of other commands to run.
dostuff.cmd sets a number of environment variables and aliases which are required for the remaining commands in parent.cmd. Therefore it is necessary that dostuff.cmd runs in the same command prompt as parent.cmd.
dostuff.cmd is written by someone else and does all sorts of things which I know nothing of. If I call some other .cmd file of my own devising in the way described above, it executes correctly and then the parent.cmd file continues executing afterwards without any problem.
Therefore something in dostuff.cmd is shutting off the processing of parent.cmd. Any ideas what this could be and how I could stop it/get around it?
Here is the parent.cmd program:
#echo off
:Begin
echo.hello
call "C:\Program Files\Prog1\bin\dostuff.cmd" -abc="def"
echo.goodbye
:End
The output is
C:\Users\cowman\desktop>.\parent.cmd
hello
dostuff.cmd text...blah blah
C:\Users\cowman\desktop>
As you can see, the echo.goodbye code is not called.
Without a listing of dostuff.cmd, we're guessing.
My patented guess would be that dostuff.cmd itself invokes cmd. If you were to respond exit to the second prompt, you should then return to the caller (unless dostuff.cmd again invokes cmd.
if the exit response terminates the cmd session and closes the window, then that's a real mystery.
This sounds that doStuff.cmd is exited by a syntax error in the batch file while redirecting stream 2 to nul.
A syntax error stops immediatly all batch instances/call stack, but the command window stays open.
I suppose the name of your batch file isn't doStuff.cmd and also the parameters are more complex than -abc="def".
As you said in the comments, the command works from the command line, but not from your parent batch, I suppose the parameters contains percent signs or carets.
If they contain percent signs, try to quadruple them.
doStuff.cmd "printf("%d",1)"
convert it to
call doStuff.cmd "printf("%%%%d",1)"
When there are carets involved try something like
set "myCaret=^"
call doStuff.cmd "a caret%%myCaret%%"
Sounds like dostuff.cmd is exiting for some reason, which will cause the whole cmd.exe to exit.
Does it have a "exit" command anywhere?, can try changing to "exit /B" to just exit the script not cmd.exe

VB6 Debugger - Command line arguements

In the VB6 IDE there is a window were you can specify command line arguements (for debugging). If you create a .exe with arguements specified, will they be published with the application? I don't believe they will.
No, they will not. This is just for testing your application if it uses command line arguments. It allows you to specify your command line arguments and step through exactly like if the program was called with those command line arguments.
For example, you normally pass parameters by simply calling your EXE and providing the parameters at the same time. So, if you normally call your program like such
C:\test.exe /test /inet /copy
You would simply set the command-line arguments to
/test /inet /copy
in the VB6 IDE

is it possible to distinguish when shell is run from command line or from other shell?

For example I can directly call myscript.cmd or in other script I can put a line to myscript.
The reason is that if a script is run on it's own it dissapears as soon as it stop executing, so I can't see the result, so at the end I must add #pause but when I run it from another shell this causes annoyance since console window wouldn't exit that way.
So I look for some kind of 'if' condition to address this issue.
To get your script paused when double-clicked (or by dropping files on it), but terminating the usual way when invoked from console:
#echo off
echo Hello World!
:: ...your ScriptCode...
(((echo.%CMDCMDLINE%)|find /I "%~0")>NUL)&&pause
Unless you create an environment variable like Stu suggested, you're not going to find any that do what you want. You're going to need to write a small program that queries the parent process programmatically and returns a value your script can check. If you're being run from Start->run your parent will be explorer.exe. Otherwise it will be cmd.exe or some other exe.
Sample code to find the parent process can be found here.
Why not set it yourself?
SET RUNNINGFROMOTHERSHELL=YES
CALL MYSCRIPT.CMD
SET RUNNINGFROMOTHERSHELL=
In MyScript.Cmd:
IF "%RUNNINGFROMOTHERSHELL%"=="" GOTO NOPAUSE
PAUSE
:NOPAUSE

Resources