i'm looking for best practice in a situation where i want to send two commands to a separate window. as in:
start /wait command1 && command2
this works however the second command is not executed in the new window instead runs in the initial window after the first task is finished.
how should a situation like this be handled correct?
You cannot do it directly with start but cmd supports that using quotes:
start /wait cmd /c "command1 && command2"
You probably can use cmd directly:
cmd /c "command1 && command2"
Related
I'm trying to java in a new cmd window which won't auto close after the execution.
Here is the code in the batch file I tried
start CMD /c "java Hello" && "pause"
I also tried
start CMD /c "java Hello" & "pause"
and
start cmd /k /c "java Hello" & "pause"
Put the whole set of commands in a single set of quotes, for example
start CMD /c "java Hello && pause"
or
start CMD /c "java Hello & pause"
The second, with a single ampersand, will still pause even if the first command fails, whereas the first will close the window before you can read any error messages.
From the Microsoft docs here
/c Carries out the command specified by string and then stops.
/k Carries out the command specified by string and continues.
I think you just need the /k without the /c. That worked in my simple experiment.
bat files from another Main.bat file
Files contain something like follows and i want both to launch in 15 seconds delayed and stay untill i close each one of them with a "Ctrl+C", can someone please help me with this Use Case please.
Main.bat
echo Task-1:
call C:\Users\user\bat\My_bat1.bat
echo Task-2:
call C:\Users\user\bat\My_bat2.bat
My_bat1.bat
start /wait cmd.exe /k "cd PATH && mvn -P dev"
My_bat2.bat
start /wait cmd.exe /k "cd PATH && mvn -Dspring.profiles.active=dev,swagger,no-liquibase -Dspring.cloud.config.profile=dev -DskipTests=true"
If you are just asking how to delay your batch files by 15 seconds you can do it like this:
choice /t 15 /D y /n
If not can you please clarify your question?
EDIT: Based on your update, you want to add pause to your first child bat file, then the code above before the call command in your parent bat file.
You should really edit your question to include the sequence of events you want from the comments because it causes a lot of confusion.
I'll try to piece together what you want:
Start job 1, creating a new window
Do not wait for job 1 to finish, instead wait 15 seconds
Start job 2, creating a new window
If you press Ctrl+C in either window it should stop the job and close the window
I assume that if one job is terminated before or after the other is started, the other still needs to start or continue to work.
First of all, the program that currently runs - in your case that seems to be maven - actually receives Ctrl+C and decides how it wants to act on it. I'm not sure if it does in the way you want it to.
Main.bat
echo Task-1:
"%comspec%" /c "C:\Users\user\bat\My_bat1.bat"
timeout 15 /nobreak
echo Task-2:
"%comspec%" /c "C:\Users\user\bat\My_bat2.bat"
I added a timeout command between the launches. It waits 15 seconds.
If you removed /nobreak it would also be possible to interrupt the wait with a keystroke launching task#2 early
I find that call sometimes introduces wierd errors in such cases so I prefer "%comspec%" /c. It does the same thing except it does not receive back environment variables set in child file which is fine in your scenario.
My_bat1.bat
start "" "%comspec%" /c "cd PATH & mvn -P dev & timeout -1 /nobreak"
I removed /wait which prevented the second mvn instance from starting until the first one is finished or terminated.
I replaced /k with /c and added timeout to pause execution in stead of /k.
The timeout -1 /nobreak statement causes command interpreter to wait indefinitely without a chance to stop it except for Ctrl+C.
You can remove /nobreak to allow it to be closed with any keystroke if mvn exited normally (if that ever happens)
"%comspec%" is the same as cmd.exe but is preferred if Microsoft ever decides to change command interpreter exe name. Empty "" before is required because start interprets first double-quoted string as a window name.
I assume there is a folder named PATH inside current folder because it is not a variable. Furthermore, variable %PATH% is reserved for executable/library search path list and must not be assigned some random value or used with cd command unless you really know what you are doing. See path /?.
I also used & instead of && to prevent window from closing if mvn crashes.
My_bat2.bat can be changed similarly.
I tried Start cmd to open new cmd prompt but i am not able to give command line arguments in new cmd ..
I tried with following
system("start cmd") >> "system("C:\\Windows\\System32\\ipconfig");
not working
system(start system("C:\\Windows\\System32\\ipconfig"));
not working
As said by Dipak D Desai, you can simply use
system("cmd /c start C:\\Windows\\System32\\ipconfig");
But if you do that in a non console application, here is what will happen :
Windows will create a new cmd windows
it will execute ipconfig in that window
it will close the window as soon as the program ipconfig has ended.
If you want the window to stay open after the end of the command, you can use :
system("cmd /c start cmd /k C:\\Windows\\System32\\ipconfig");
The first cmd /c allows to pass the command start that is an internal command. The second cmd /c (or cmd /k) starts a new shell (cmd.exe) but ask it not to close after executing first command, but instead to open a command loop.
In fact, the first cmd /c is not necessary, since it is implied by the system call. So it should be omitted from the command even if is is harmless (thanks to #eryksun for noticing)
Launch a separate CMD Windows, you need to call cmd.exe:
system("cmd.exe /c C:\\Windows\\System32\\ipconfig");
I have a bat file like this:
ipconfig
That will print out the IP info to the screen, but before the user can read that info CMD closes itself.
I believe that CMD assumes the script has finished, so it closes.
How do I keep CMD open after the script is finished?
Put pause at the end of your .BAT file.
Depending on how you are running the command, you can put /k after cmd to keep the window open.
cmd /k my_script.bat
Simply adding cmd /k to the end of your batch file will work too. Credit to Luigi D'Amico who posted about this in the comments below.
Just add #pause at the end.
Example:
#echo off
ipconfig
#pause
Or you can also use:
cmd /k ipconfig
When the .bat file is started not from within the command line (e.g. double-clicking).
echo The echoed text
#pause
echo The echoed text
pause
echo The echoed text
cmd /k
echo The echoed text & pause
Adding pause in (Windows 7) to the end did not work for me
but adding the cmd /k in front of my command did work.
Example :
cmd /k gradlew cleanEclipse
start cmd /k did the magic for me. I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it. Below is the simple example
start cmd /k echo Hello, World!
What I did use in my case
start cmd /k cordova prepare
Update
You could even have a title for this by using
start "My Title" echo Hello, World!
If you are starting the script within the command line, then add exit /b to keep CMD opened
In Windows add '& Pause' to the end of your command in the file.
I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first.
What you need to do is type the full command along with cmd /k. For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:
cmd /k javac my_code.java
So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.
javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java
cmd cd C:\xxx\yourbat.bat
the second command make your cmd window not be closed.
The important thing is you still able to input new command
As a sidenote this also works when running a command directly from the search bar in windows.
e.g. directly running ipconfig will directly close the cmd window after the command has exited.
Using cmd \k <command> won't - which was what i was trying to do when i found this answer.
It has the added advantage of always recognizing the command you're trying to run. E.g. running echo hello world from the searchbar won't work because that is not a command, however cmd \k echo hello world works just fine.
I already know how to create a new cmd window from a batch script with custom color, and a new cmd window with a custom prompt. However am wanting to find a way of combining the two together...
Here is what I have in my batch file to create a new cmd window with customised prompt (in this case, the customised prompt is the windows version details):
start cmd /k "prompt $v"
... And this is what I'm doing to create a new cmd window with customised color:
start cmd /k "color 42"
I've tried the following to combine the two, but none of them work:
start cmd /k "color 42" /k "prompt $v"
start cmd /k"color 42" "prompt $v"
If anyone can help point me in the right direction that would be awesome. Been searching via Google and other forums but after spending over an hour on a fruitless search I thought I'd ask a question here...
The only thing you are missing is the operator that will concatenate multiple commands on one line: &.
start cmd /k "color 42&prompt $v"
This operator works in all situations, not just within the command string for the CMD command. There are a few concatenation operators with different behavior:
& - Always executes the next command
&& - Only executes the next command if the prior command was successful (ERRORLEVEL=0)
|| - Only executes the next command if the prior command failed (ERRORLEVEL<>0)
try:
start cmd /k"color 42; prompt $v"
I'm late to the party here, but note that cmd /t:fg will set the colors just like the color command, so you could also run
start cmd /t:42 /k "prompt $v"