3

I want to open a new shell and pass a command for it to execute in a single line of code from the windows cmd window. What is the easiest way to accomplish this?

For example I have a cmd shell and I want to execute:

C:\app\cmd.exe THEN "run_app.exe argument1"
Rob
  • 3,333
  • 5
  • 28
  • 71
  • 1
    Possible duplicate of [How to run a command on command prompt startup in Windows](http://stackoverflow.com/questions/17404165/how-to-run-a-command-on-command-prompt-startup-in-windows) – Ryan Bemrose Jul 19 '16 at 17:29
  • it's a specific cmd prompt, not just the standard one, – Rob Jul 19 '16 at 17:35

1 Answers1

3
cmd /c run_app.exe argument 

to close after executing or

cmd /k run_app.exe argument

to keep open after executing.

If in doubt, use full paths to your executable:

cmd /c c:\path\to\run_app.exe argument

To run several commands one after another, use chaining:

   cmd /k run_app.exe argument & second.exe & third.exe
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • so the issue I'm getting is that i run the first command "C:\Windows\SysWOW64\cmd.exe /k cd C:\Program Files\myapp" then I want to run "execute_app.exe arg1" afterwards but when I add the -k it says "the filename, directory name or volume label syntax is incorrect" – Rob Jul 19 '16 at 17:36
  • Why `cd` there and not directly run `cmd /k c:\path\to\run_app.exe`? – Thomas Weller Jul 19 '16 at 17:41
  • Lots of applications require a particular current directory, often but not necessarily the one the executable lives in. The simplest approach is usually to set the current directory before calling `cmd /c` and let the child shell inherit it. – Harry Johnston Jul 19 '16 at 23:42