BAT file: open a new cmd window and enter the code there

I am trying to work with a .BAT file, it should be a very simple script, but I do not know how to do it (I am not familiar with it).

I am trying to open a new command window: start% windir% \ system32 \ cmd.exe

And the code that I print after it (for example, a printout) should appear there: echo "test in new window"

How can i do this?

Therefore, I need to do the encoding in a new command window. Can someone help me with this?

+133
command-line new-operator cmd batch-file
Feb 22 '12 at 10:18
source share
8 answers

You may have already found your answer, because it was some time ago when you asked. But I tried to do something similar when coding. I wanted to run the "rails server" in a new cmd window, so I do not need to open a new cmd, and then search my path again.

What I found out is to use switch K as follows:

start cmd /k echo Hello, World! 

start before "cmd" opens the application in a new window and "/ K" executes "echo Hello, World!". after the new cmd got up.

You can also use the / C switch for something like that.

 start cmd /C pause 

Then a β€œpause” will be executed, but close the window when the command is completed. In this case, after pressing the button. I found this useful for the "rails server", and then when I shut down my dev server, I do not need to close the window after.

+218
Jul 09 2018-12-12T00:
source share

In your batch file, use the following:

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

or

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

/ c Executes the command specified by the line and then ends
/ k Executes the command given by the string, but remains

See the cmd.exe documentation for more information using cmd /? .

Correct formatting of the command line is a little complicated by spaces in the arguments. See Examples below. Note the use of nested double quotes in some examples.

Examples:

Run the program and pass the file name parameter:
CMD /c write.exe c:\docs\sample.txt

Run the program and skip the long file name:
CMD /c write.exe "c:\sample documents\sample.txt"

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

Spaces in the 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""

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

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

+70
Feb 09 '15 at 23:33
source share

It is not very simple.

The best approach is to put the part of your script that you want to execute in the "new window" into a separate .bat file. This can be impractical if, for example, you need a lot of states from the rest of your script (variables, etc.). One option is to pass any required values ​​(for example, dir to work) to a batch file:

 start cmd.exe stuff.bat %this_dir% 

If you have a large number of transfer states, you might consider creating 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.

+11
Feb 22 2018-12-22T00:
source share

Thanks to everyone here at Stack Overflow; This solution solves the above question, but extends to the automatic execution of these tasks:

  • I want to start my rails server
  • Run rake jobs: worker for my delayed_job gutter.
  • and open the default internet browser to show my page
  • finally leave the cmd window for any additional commands during my session.

I guess my project is called "antiquorum."

Create the file "init.bat" in the % USERPROFILE% directory (open the cmd window and look at the path to the left of the cursor to find out that %% USERPROFILE%)

 @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 window, the command prompt window: C:> init antiquorum

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

Section : start does the job. You can run tasks 1,2 or 4 separately by entering parameters like: server , worker , or no one will leave CMD open in root "anticorum" .

Enjoy.

+5
Sep 08 '13 at 0:51
source share

The above answers helped me. But still it took some clarification. Here is an example script I use to run 3 processes for web development. This leads to the fact that 3 windows remain open, as they must work continuously.

Mongo is globally added to my path, so I don't need to write cd like I do for two other programs. Of course, the path to your files will be different, but I hope this helps.

 :: 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" 
+5
Jul 6 '17 at 14:54
source share

If I understand that you are doing this correctly, your bat file will open a command prompt and print your message on the screen.

 cmd.exe hello world 

hope this helps.

+2
Feb 22 '12 at 10:27
source share

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

 start cmd.exe /k python "C:\Program Files\HelloWorld.py" 
+1
Feb 19 '19 at 2:30
source share

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

Example:

 cmd /k echo "hello" 

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

0
Jun 23 '19 at 21:07 on
source share



All Articles