use root cmd window to execute commands in new cmd window - cmd

i'm trying to make a batch script for running my Java files. I've found out that there is no way to prevent auto-closure of a batch script(except using pause keyword, tho it just waits for key press). I've also discovered that starting a new window will cause only main windows to close, not the new one too so i want a way that the command SET /P file=Java file: is executed in the new window(I got the new window by using the start keyword. Is there any way to accomplish this without downloading other softwares? this is the code i came up with yet:
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
set /P file=Java file to execute:
java %file%^.jar
start

I guess you're looking for that :
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
start cmd /V:ON /K "#set /P "file=Java file to execute: " && java -jar !file!^.jar"
EDIT: Using expansion with /V and use of /K instead of /C to keep the cmd windows open.
Explanations : To launch a console process in another windows and keep it open after the end of this process console we launch another cmd process with the start command. We use /V:ON to use delayed expansion by default, meaning modified variables (like the one we prompt, %file%) will be expanded on the fly with ! (so !file! instead of %file%). We use /K to tell to this cmd process to not close when provided commands end. To this cmd process, we provide the following commands :
#set /P "file=Java file to execute: "
This one will ask for the jar filename (without extension) to launch.
The # means "do not echo the command itself on the console stdout" for a nice display.
java -jar %file%^.jar
This one launch the java interpreter (JVM) with the filename of a jar file to execute through the -jar parameter, filename build from the previous prompt and the .jar extension. The ^ here escapes the ., it seems not useful here but maybe your script/env requires it.
We link the both commands with && which means : _if left command from && is successful (it exits with ERRORLEVEL 0) then execute the right command from &&.

Related

.cmd file on Win10 - How to "multi thread" to have actions fire concurrently? [duplicate]

Say, if I have
foo.exe
bar.exe
baz.exe
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
Using the START command to run each program should get you what you need:
START "title" [/D path] [options] "command" [parameters]
Every START invocation runs the command given in its parameter and returns immediately, unless executed with a /WAIT switch.
That applies to command-line apps. Apps without command line return immediately anyway, so to be sure, if you want to run all asynchronously, use START.
Combining a couple of the previous answers, you could try start /b cmd /c foo.exe.
For a trivial example, if you wanted to print out the versions of java/groovy/grails/gradle, you could do this in a batch file:
#start /b cmd /c java -version
#start /b cmd /c gradle -version
#start /b cmd /c groovy -version
#start /b cmd /c grails -version
If you have something like Process Explorer (Sysinternals), you will see a few child cmd.exe processes each with a java process (as per the above commands). The output will print to the screen in whatever order they finish.
start /b : Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application
cmd /c : Carries out the command specified by string and then terminates
You can use the start command to spawn background processes without launching new windows:
start /b foo.exe
The new process will not be interruptable with CTRL-C; you can kill it only with CTRL-BREAK (or by closing the window, or via Task Manager.)
Create a batch file with the following lines:
start foo.exe
start bar.exe
start baz.exe
The start command runs your command in a new window, so all 3 commands would run asynchronously.
Use the START command:
start [programPath]
If the path to the program contains spaces remember to add quotes. In this case you also need to provide a title for the opening console window
start "[title]" "[program path]"
If you need to provide arguments append them at the end (outside the command quotes)
start "[title]" "[program path]" [list of command args]
Use the /b option to avoid opening a new console window (but in that case you cannot interrupt the application using CTRL-C
There's a third (and potentially much easier) option. If you want to spin up multiple instances of a single program, using a Unix-style command processor like Xargs or GNU Parallel can make that a fairly straightforward process.
There's a win32 Xargs clone called PPX2 that makes this fairly straightforward.
For instance, if you wanted to transcode a directory of video files, you could run the command:
dir /b *.mpg |ppx2 -P 4 -I {} -L 1 ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"
Picking this apart, dir /b *.mpg grabs a list of .mpg files in my current directory, the | operator pipes this list into ppx2, which then builds a series of commands to be executed in parallel; 4 at a time, as specified here by the -P 4 operator. The -L 1 operator tells ppx2 to only send one line of our directory listing to ffmpeg at a time.
After that, you just write your command line (ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"), and {} gets automatically substituted for each line of your directory listing.
It's not universally applicable to every case, but is a whole lot easier than using the batch file workarounds detailed above. Of course, if you're not dealing with a list of files, you could also pipe the contents of a textfile or any other program into the input of pxx2.
I could not get anything to work I ended up just using powershell to start bat scripts .. sometimes even start cmd /c does not work not sure why .. I even tried stuff like start cmd /c notepad & exit
start-Process "c:\BACKUP\PRIVATE\MobaXterm_Portable\MobaXterm_Portable.bat" -WindowStyle Hidden

Making a batch file for multiple CMDs

Lets say I have 2 services I want to start and a property file.
-svc1.cmd
-svc2.bat
-svc2prop.properties
Lets have directories as C:\program1\bin\ and C:\program2\bin\ + C:\program2\config\
then I tried to do it like this:
start cmd /k call C:\program1\bin\svc1.cmd
start cmd /k cd C:\program2\ .\bin\svc2.bat .\config\svc2prop.properties
I can start them both separately by opening a local CMD. The thing is, if I am in the directory C:\program2\ I can open up a local CMD and run this statement without problems " .\bin\svc2.bat .\config\svc2prop.properties"
But I want to create a batch file that: first, opens a new cmd and starts the svc1.cmd, then opens another cmd in which it goes to C:\program2\ and runs the "" .\bin\svc2.bat .\config\svc2prop.properties" " statement ... but for some reason it doesnt work.
Any possible solution ?
the second line should use /D option to start the process in the required directory:
start /D C:\program2 cmd /k .\bin\svc2.bat .\config\svc2prop.properties
(in your example, you were just passing a lot of arguments to an useless cd command)
If you have some current directory problems with the first line, just do the same:
start /D C:\program1\bin cmd /k call svc1.cmd

cmd.exe doesn't terminate when using a .bat file

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

Launch new command line and exucte in that shell

This must be a real dumb question. And I cant figure out how.
What I want to do is launch a new command line with some arguments and excute some commands there.
Here is what I have in my .cmd
CMD /k %EnvInstallPath% %wo% %var%
cd /d %wo%\src
When I execute test.cmd, I see that the directory changes to %wo%, but the further cd src is not executed. I need to cd to a directory and execute few other commands.
When you run cmd with /k the console runs the command and then resumes with the prompt. I'm guessing that what you want is to run the command and resume with the next one, so you need to run cmd with /c instead.
put the other commands in a different bat file and
start AFewOtherCommands.bat

How to prevent auto-closing of console after the execution of batch file

What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?
In Windows/DOS batch files:
pause
This prints a nice "Press any key to continue . . . " message
Or, if you don't want the "Press any key to continue . . ." message, do this instead:
pause >nul
Depends on the exact question!
Normally pause does the job within a .bat file.
If you want cmd.exe not to close to be able to remain typing, use cmd /k command at the end of the file.
If you want cmd.exe to not close, and able to continue to type, use cmd /k
Just felt the need to clarify what /k does (from windows website):
/k : Carries out the command specified by string and continues.
So cmd /k without follow up command at the end of bat file will just keep cmd.exe window open for further use.
On the other hand pause at the end of a batch file will simply pause the process and terminate cmd.exe on first button press
If you are using Maven and you want to skip the typing and prevent the console from close to see the result you need to use the CALL command in the script, besides just the 'mvn clean install'.
Like this will close the console
ECHO This is the wrong exemple
mvn clean install
pause
Like this the console will stay open
ECHO This is the right exemple
CALL mvn clean install
pause
If you dont use the CALL command neither of the pasts exemples will work. Because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe
The below way of having commands in a batch file will open new command prompt windows and the new windows will not exit automatically.
start "title" call abcd.exe param1 param2
start "title" call xyz.exe param1 param2
Add cmd.exe as a new line below the code you want to execute:
c:\Python27\python D:\code\simple_http_server.py
cmd.exe
my way is to write an actual batch (saying "foo.bat") to finish the job; then create another "start.bat":
#echo off
cmd /k foo.bat
I find this is extremely useful when I set up one-time environment variables.
Call cmd at the end of the batch file.
Had problems with the answers here, so I came up with this, which works for me (TM):
cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause
besides pause.
set /p=
can be used .It will expect user input and will release the flow when enter is pressed.
or
runas /user:# "" >nul 2>&1
which will do the same except nothing from the user input will be displayed nor will remain in the command history.
This little hack asks the user to enter a key and stores it into the variable %exitkey% (this variable could be called anything you like though).
set /p exitkey= "Press any key to continue..."
NB: the space after the '=' is very important
I know I'm late but my preferred way is:
:programend
pause>nul
GOTO programend
In this way the user cannot exit using enter.
Possibility 1:
Just make 2 .bat files and write into the first:
start <filename> // name of 2nd batch file
exit
Batch file 2 is the file that wont close in the end.
So now when you open batch nr.1 It will start the 2nd and cloe itself.
When the 2nd finishes it will not close entirely (as long as you wont put exit at the end)
Possibility 2:
Batch file 1:
call <filename>
cls
echo End of file
pause
<any code you want>
When the 2nd file ends then it will proceed to file 1 again and output the rest of it. With that you can even make error handlers. If nr.1 crashes it goes into nr.2 and displays it
There are two ways to do it depend on use case
1) If you want Windows cmd prompt to stay open so that you can see execution result and close it afterwards; use
pause
2) if you want Windows cmd prompt to stay open and allow you to execute some command afterwords; use
cmd
pause
or
echo text to display
pause>nul
Easy, add cmd to your last line of bat, BUT! if you reset or clear your system path, you must start your cmd with the full path, like:
%windir%\system32\cmd.exe
For example, I have a bat file to reset jdk to old version like this:
PATH=C:\Program Files\Java\jdk1.6.0_45\bin;C:\apache-ant-1.7.1\bin
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45
%windir%\system32\cmd.exe
since I reset the system path, I have to run cmd with the full path, or the system can't find cmd.exe, it will fail to run cmd, and just close the window, and you can't see the error msg.
Run the .exe file and then pause the cmd
batch script example :
#echo off
myProgram.exe
PAUSE
batch script example with arguments :
#echo off
myProgram.exe argumentExample1 argumentExample2
PAUSE
I added #echo off because I don't want to show C:\user\Desktop>myProgram.exe and C:\user\Desktop>PAUSE in the cmd
cmd /k cd C:\Projects.....
If you want your cmd opened at specific long location
add pause (if you don't want anything else to show up add) >nul it should look like:#echo offtitle niceecho hellopause >nulall you will see is "hello"
I personally put pause >nul and it waits for a key to be pressed without showing any extra text in the console.
using : call yourbatch.cmd
does the job
will process the script and then continue ejecuting other code on same window (cmd instance)

Resources