Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a PAUSE command to a START command in a batch file

Tags:

batch-file

cmd

Let's say I have a batch file with a bunch of lines each starting with START to run commands simultenously and I want each a new window that pops up to just pause when it's finished, instead of just closing, so that I could read the summary at the end.

start myapp.exe && pause doesn't work as the pause command just gets executed in the main window and doesn't get passed down with START. CMD /k works to prevent the window, but what I'd like to avoid that and use PAUSE.

It's important that I run these simultanously and I don't want to create a separate batch file for each line.

Any suggestions?

like image 457
Tarhonyaa Avatar asked Oct 20 '25 15:10

Tarhonyaa


1 Answers

The start command can only be used invoke a single internal or external command. To pass additional commands you have to pass the commands to a new instance of CMD and escape any special characters, to be able to pass them to the child process (CMD.EXE in this case).

start cmd /c myapp.exe ^& pause


As an addition, If command extensions are enabled (which is the default case) "CMD " can be used instead of just cmd or cmd.exe,...

Note that it has to be "CMD " with an extra space after CMD so it is different from "CMD"

Quoted from the start command help:

If Command Extensions are enabled, external command invocation through the command line or the START command changes as follows:
.
.
.
When executing a command line whose first token is the string "CMD " without an extension or path qualifier, then "CMD" is replaced with the value of the COMSPEC variable. This prevents picking up CMD.EXE from the current directory.

So a safer approach would be

start "" "CMD " /c myapp.exe ^& pause

It is functionally equivalent to

start "" "%COMSPEC%" /c myapp.exe ^& pause

And because the first quoted argument to the start command will be interpreted as the window title, A dummy title (in this case an empty title "") was passed as the first argument of the start command.

like image 90
sst Avatar answered Oct 23 '25 15:10

sst