All
I'm trying to work with a .BAT file here, it should be a very simple script but I don't know how to do it (I'm not familiar with it).
What I'm trying to do is open a new command window: start %windir%\system32\cmd.exe
And the code that I type behind it (a print for example) should show up in there: echo "test in new window"
How can I do this?
So I need do to some coding in a NEW command window. Can anyone help me on this?
Kind regards
If I understand you correctly doing this in side your bat file will open Command prompt and print your message to screen.
hope this helps.
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.
Use the following in your batch file:
or
Consult the cmd.exe documentation using
cmd /?
for more details.The proper formating of the command string gets a little more complicated with spaces in the arguments. See the examples below. Note the use of 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 long filename:
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
Thanks to all here in Stack Overflow; this solution solves the above question but is extended to automatically run these tasks:
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)
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.
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:
If you have a large amount of state to transmit you might consider generating a batch file at runtime:
Obviously this is a trivial example.
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 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.
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.