BAT file: Open new cmd window and execute a command in there

Tjekkles picture Tjekkles · Feb 22, 2012 · Viewed 506.1k times · Source

I'm trying to open a new command window in a BAT file:

start %windir%\system32\cmd.exe

After it opens, I'd like to execute a BAT command in the new window:

echo "test in new window"

How can I do this?

Answer

aross picture aross · Jul 9, 2012

You may already find your answer because it was some time ago you asked. But I tried to do something similar when coding ror. I wanted to run "rails server" in a new cmd window so I don't have to open a new cmd and then find my path again.

What I found out was to use the K switch like this:

start cmd /k echo Hello, World!

start before "cmd" will open the application in a new window and "/K" will execute "echo Hello, World!" after the new cmd is up.

You can also use the /C switch for something similar.

start cmd /C pause

This will then execute "pause" but close the window when the command is done. In this case after you pressed a button. I found this useful for "rails server", then when I shutdown my dev server I don't have to close the window after.