I was playing around with Batch on my computer, and I wanted to run a process in its own window. I figured out how to get it to run in its own window, but I want the window to close when the process ends. How can I do this?
This code will run python 2's python.exe in its own window:
#echo off
start call "C:\Program Files\Python27\python.exe" %*
Is there a way I can assign the new window to some variable and check if it has finished running the process, then exit the new window?
START via CMD /C, and the window will close once the command ends.
Also, as Endoro said in his comment, you should not need CALL
#start cmd /c "C:\Program Files\Python27\python.exe" %*
Related
I want to make a batch file that runs a particular program and then the command window exits itself, I tried this cause i will make a shortcut of that batch file so the batch file is in root directory
#echo off
"program.exe" "mainframe.pkg"
exit
it works but the black windows doesn't disappear and causes a fuss in the program cause it has perimeters. Any way to remove the black ugly CMD window.
Use the start command.
#echo off
start "" "program.exe" "mainframe.pkg"
The first quoted string after the start command is a console window title (if you are starting a console program); it can be an empty string as in my example. After that, specify the program name and its parameters.
You do not need the exit command at the end of the script. (In fact, I recommend against it without the /b parameter, because if you run the script from a cmd.exe prompt, your cmd.exe window will close without warning.)
You need to add exit 0 to the end of your program like so:
#echo off
start "program.exe" "mainframe.pkg"
exit /B 0
This should work, but let me know!
#echo off
start /B "" "program.exe" "mainframe.pkg"
exit /B 0
What I got is a list of bat files:
file1.bat
file2.bat
…
file29.bat
I need them to run one after each other. Meaning when the file1.bat closes file2.bat starts and so on.
I tried this, but it doesn't work properly:
start /wait call file1.bat
start /wait call file2.bat
You might want to add more to your question to make it easier to understand. My guess is that you want the bat file to open the next one then close itself after that.
If that's what you want to do; add these commands to each of the files:
start file2.bat
exit
Of course, you'll want to change start file2.bat to start file3.bat and so on for each file.
If you want file1.bat to manage all of the files, I don't think that's possible in batch.
You didn't describe how exactly it's not doing what you expected. I'm guessing that what happens is you're having to shut down each script before the next one will continue.
Documentation on start says:
WAIT Start application and wait for it to terminate.
command/program
If it 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 you must use start then you could force it to use the /c switch which will automatically close the window once it's done:
start /wait cmd /c call file1.bat
I'm not really sure you accomplish anything by using call so that ought to be equivalent to just:
start /wait cmd /c file1.bat
Using start creates a new window for each program and you may just want to have it all run in a single command processor window.
As noted by Biffin you can just list them all out in a master script and they will run in order.
call file1.bat
call file2.bat
...
call file29.bat
And a shorthand for that is:
for /l %%f in (1; 1; 29) do call file%%f.bat
Remember to double up those percent characters inside a batch script but not on the command line.
This question might explain some of the unexpected behavior you were seeing.
How to run multiple .BAT files within a .BAT file
I'm trying to write batch script to create a folder if it does not already exist.
Following up the online examples, below is my script.
The problem is; first pause works, then probably due to syntax error the window closes even before reaches to the second pause, so I can't really tell which part of my script is wrong.
Could anyone show me how to prevent closing window so that I can see what's on the window?
#echo off
:copy theme images over
:designer
echo copying theme images over...
pause
if not exist "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text"
(
md "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text333"
)
pause
You could put this line at the beginning of the batch file:
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
What this line does is, the first time you run it, it re-launches itself in a subprocess that doesn't exit after it finishes running the batch file.
You need to pass the /K switch to CMD, or just open a Command Window and run the batch from the command line.
Press start and type cmd and press enter, you will launch a command prompt.
Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter.
(You might some time to run the cmd as admin: find the cmd in the start menu, right-click on it, choose run as admin).
I recorded the screen (bandicam) for when I couldn't quite read the error message, and then I could replay it; I suppose this is mainly helpful if you already have software on your computer.
using pause at end of batch paused the cmd screen, tanks!
How to prevent batch window from closing when error occurs?
I had the problem when using robocopy. My solution was:
if not %errorlevel% lss 8 pause
For Robocopy every exit code below 8 is a success:
https://ss64.com/nt/robocopy-exit.html
I need to start an exe from a batch file and after it has run it, I want the CMD prompt to exit, without waiting for the application to exit.
By just entering the location of the EXE does not work, it seems to wait for it to exit.
Is there a way to not do that with the START command?
Use CALL instead of START
call notepad.exe
echo byby
exit
I know it seems this has been asked before, but I need a batch to open another batch in a new window. I've tried:
start abc.bat
cmd abc.bat
run abc.bat
and others. They've all opened in the same window or just opened Command Prompt in new window, ignoring my batch. Is there a batch command to open a batch file in a new window?
Is this what your after?
start "New Window" cmd /c test.cmd
It's a little bit strange that start abc.bat doesn't work but I assume this is because you are running this in the middle of another batch. You probably need call:
22:22:38.85 c:\help call
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
Giving you start call abc.bat or call start abc.bat depending on what the exact problem is.
To simply do it is just
start cmd /c "exampleexample.bat"
This could also work with spaces;
start cmd /c "example example.bat"
And directories.
start cmd /c "C:\NAME\Example\Hi there\example example.bat"
I created my universal batch with this and this works flawlessly.
start abc.bat works for me. What is the problem in your case? You could also try start cmd /c abc.bat.
I found something that works:
call "C:\FILEPATH HERE\abc"
Demo:
#echo off
call "C:\Users\USERNAME HERE\Desktop\abc"
This should work <3
Unfortunatly, I know of no such method (I encounter the same thing). However, try killing the old window when you start the batch
abc.bat:
abd.bat
stop
abd.bat:
#echo off
#echo It works!
If you are going to run it in a different command prompt, type start C:\abc.bat or whatever the directory of abc.bat is, or if you want to open it in the same command prompt, type call "C:\abc.bat" again, wherever the directory is. It should work
Either:
call "C:\abc.bat"
or
start C:\abc.bat