22

I have a case where I need to spawn multiple CMD instances (using the START program), and each instance needs to run some commands in sequence. These commands are generated by a batch script, so they are not known ahead of time.

Basically, what I'm looking to do is something like the following, but I don't know the proper syntax (or if it's even possible):

START (program_a && program_b && program_c)

Obviously, those parentheses are incorrect syntax. So when I try to run some syntactically correct variant(s):

START program_a && program_b && program_c

I just end up with one CMD instance being spawned, running program_a, and the "owning" batch script continues to execute program_b and program_c on its own (i.e. not in the CMD instance spawned by START).

3
  • 1
    Put several Start commands on their own line. Why do they have to be executed in the same cmd process ? Why not create a second batch dynamically that calls those programs ? Commented Jan 9, 2016 at 20:02
  • 2
    Because the tasks cannot be parallelized. They must be run in sequence. Commented Jan 9, 2016 at 20:13
  • 1
    @Marged suggested that you dynamically create a subsidiary batch file containing the three critical lines on separate physical lines, then start the subsidiary batch. That would run the three commands in sequence. You should add the /wait switch to the start if you want the main batch to wait for the subsidiary batch to finish. Commented Jan 9, 2016 at 20:24

2 Answers 2

41

I think, you need something like:

start "MyWindow" cmd /c "ping localhost & ipconfig & pause" 
Sign up to request clarification or add additional context in comments.

4 Comments

That's not work for me because one command need quotes also like dir "c:\foo 1".
@gotwo: Where's the problem? start "MyWindow" cmd /c "ping localhost & dir "c:\foo 1" & pause". see cmd /? for how it treats quotes.
@ Stephan, yes you are right. There may be a mistake in my test before. dir... should be a simpler example on my actual topic with Powershell. Again a simplified example: start "MyWindow" cmd /k "ping localhost & powershell -command "&{$host|gm}" & pause" does not work for me. powershell -command "&{$host|gm}" works as expected. I have described here the solution.
@gotwo: Batch syntax/escaping special chars sometimes is a bit unintuitive: start "MyWindow" cmd /k "ping localhost & powershell -command "^&{$host^|gm}" & pause"
9

It helps to escape every & with ^&:

START program_a ^&^& program_b ^&^& program_c 

1 Comment

Thats exactly what i was looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.