252

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?

11 Answers 11

377

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If you're using this to run a rails server you actually don't need to type cmd. You can just type start rails s.
Just as a note i use to remember the difference, /k stands for "keep", /c stands for "close"
166

Use the following in your batch file:

start cmd.exe /c "more-batch-commands-here" 

or

start cmd.exe /k "more-batch-commands-here" 
  • /c run command then close the terminal window leaving your desktop clean.
  • /k run command then keep the terminal window open.

The /c and /k options controls what happens once your command finishes running.

Consult the cmd.exe documentation using cmd /? for more details.

Escaping Commands with White Spaces

The proper formatting of the command string becomes more complicated when using arguments with spaces. See the examples below. Note the nested double quotes in some examples.

Examples:

Run a program and pass a filename parameter:
CMD /c write.exe c:\docs\sample.txt

Run a program and pass a filename which contains whitespace:
CMD /c write.exe "c:\sample documents\sample.txt"

Spaces in program path:
CMD /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""

Spaces in program path + parameters:
CMD /c ""c:\Program Files\demo.cmd"" Parameter1 Param2
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""

Launch demo1 and demo2:
CMD /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""

Source: http://ss64.com/nt/cmd.html

2 Comments

it's the /k that will keep the cmd window open and allow for several other commands to be run manually.
There is one more diff between /k and /c. If you're using /c the new terminal window name is the same as the standard one, smth like "c:\Windows\system32\cmd.exe". but in case of /k it corresponds to the command you executed. Usefull when you need to execute multiple new terminal windows and differentiate between them for some reason.
27

The above answers helped me. But still required some figuring out. Here is an example script I use to start 3 processes for web development. It results in 3 windows staying open, as they need to run continously.

Mongo is globally added to my path, so I don't need to cd like I do for the other two programs. Of course the path to your files will vary, but hopefully this will help.

:: Start MongoDB start cmd.exe /k "mongod" :: cd app directory, and start it cd my-app start cmd.exe /k "npm run dev" :: cd to api server, and start that cd ../my-app-api start cmd.exe /k "npm run dev" 

1 Comment

this is what i exactly wanted to do, mongod, my react app and my backed server.
15

This is not very easy.

The best approach is to have the part of your script that you want to be executed in a "new window" to be in a separate .bat file. This might be impractical if e.g. you need a lot of state from the rest of your script (variables, etc). One option is to pass any values you need (e.g. dir to operate in) to the batch file:

start cmd.exe stuff.bat %this_dir% 

If you have a large amount of state to transmit you might consider generating a batch file at runtime:

set foo=Hello, World set list_me=%userprofile% set tmpdir=c:\windows\temp set tmp=%tmpdir%\tmp.foo del /q /f "%tmp%" echo.echo %foo%>>"%tmp%" echo.dir "%list_me%">>>"%tmp" start cmd.exe "%tmp%" del /q /f "%tmp%" 

Obviously this is a trivial example.

Comments

7

Thanks to all here in Stack Overflow; this solution solves the above question but is extended to automatically run these tasks:

  1. I want to run my rails server
  2. Run a rake jobs:worker for my delayed_job gem too
  3. and Open default internet browser to show my page
  4. finally, to leave a cmd window open for any extra commands during my session.

I guess my project is called "antiquorum."

Create an "init.bat" file in your %USERPROFILE% directory (open a cmd window and take a look at the path to the left of the cursor to know what %USERPROFILE% is)

@echo off cd C:/projects/rails3/antiquorum if "%1" == "antiquorum" GOTO start if "%1" == "worker" GOTO worker if "%1" == "server" GOTO server if "%1" == "" GOTO end :start start cmd /k %USERPROFILE%\init.bat worker start cmd /k %USERPROFILE%\init.bat server TIMEOUT 30 start "" "http://localhost:3000/" GOTO end :server rails s GOTO end :worker rake jobs:work :end 

In a new command line window type: C:> init antiquorum

The code opens two more cmd windows and a browser. TIMEOUT avoids errors in the browser.

The :start section does the work. You can run tasks 1,2 or 4 separately by typing params as: server, worker, or none to leave a cmd opened in root of "antiquorum" project.

Enjoy.

Comments

4

Adding /k between two commands executes both command in order.

Example:

cmd /k echo "hello" 

this command will first open command prompt then execute echo "hello" command

Comments

3

If I understand you correctly doing this in side your bat file will open Command prompt and print your message to screen.

cmd.exe hello world 

hope this helps.

1 Comment

I've added the echo command in as 'hello' isn't a command in CMD.
3

to run a python file in a new cmd window with spaces in the file name:

start cmd.exe /k python "C:\Program Files\HelloWorld.py" 

Comments

2

Extending answer from @Dan Zuzevich. Below is CD "Change Directory" if your target file is on different drive. For example it have to call file from drive C and drive D.

:: Start MongoDB start cmd.exe /k "mongod" :: cd app directory, and start it cd C:\project\dev C: start cmd.exe /k "npm run dev" :: cd to api server, and start that cd D:\api\server D: start cmd.exe /k "npm run dev" 

Comments

2

I needed to run an environment and then use python while in that environment.

If my folder structure is:

C:\environments> C:\environments\encrypted_stuff> C:\environments\encrypted_stuff\p1> C:\environments\encrypted_stuff\p2> 

And I wanted to activate the "encrypted_stuff" environment then call a script from p2, I might type this in a bat that is stored in C:\environments\p2.bat

start cmd /k "Scripts\activate && cd p2 && python p2_script.py" 

Then I can have a bat file for any of the scripts and all would use my "encrypted_stuff" environment. Now I can click the bat file from the desktop and it will load my specific environment and then run the script in that environment.

Comments

1

I wanted my window to remain open after I killed or restarted Firebase so I used two batch files.

Desktop file for easy access: run_firebase.bat:

--------------START FILE CONTENTS-------------- start cmd /k C:\dev\init_firebase_batch.bat ---------------END FILE CONTENTS--------------- 

Batch file to run the intended results: C:\dev\init_firebase_batch.bat

--------------START FILE CONTENTS-------------- cd C:\dev\aptr_datasync\aperture-customer-hub firebase emulators:start ---------------END FILE CONTENTS--------------- 

So ... double click run_firebase.bat and it runs the second batch. After Ctrl+C out of my process the window remains open. Seems trivial but I don't like distractions while I'm working on code.

I only spent time solving because I thought it would be simple. Hopefully this creates that simplicity for others.

I think this checks off the questions in the initial post:

[x] I'm trying to open a new command window in a BAT file
[x] After it opens, I'd like to execute a BAT command in the new window

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.