I have a *.exe console file.
I enter my inputs, and everything is great.
but when I enter the last input, the command window closes (because the program has ended)
before I can read the last output.
is there a way to run that *.exe file and force it to stay open after the program ends?
note: this is not my program. I can't edit the source code, so I'm not looking for answers like "add while(1) or scanf at the end".
Thanks ahead.
Just open a command prompt and run it, the way it's meant to be used.
Start -> Run -> cmd.exe
or Win+R -> cmd
Run the program from command prompt (Start-Run-cmd.exe)
Make a batch file (*.bat), with the command you want to execute, followed by pause:
myconsoleapplication.exe
pause
Save it, and run. The command window wil wait for enter to be pressed before closing.
Run it from the console, or in a batch file.
Start >> Run >> cmd.exe
Then the console window is already open.
This should work:
system("pause");
At the top of your program, include stdlib.h:
#include <stdlib.h>
You can open a command prompt window and navigate (the cd command) to the directory containing the .exe file. Then, run the program by typing its name. The window will not close after the program finishes running.
You could execute this program from one of your own, redirecting stdio and adding your own pause after it terminates. I've done this with .NET but assume that it can be done through other means. Of course a batch file, as mentioned already, is wicked simpler.
Related
I need help writing a batch file that starts a program when I close the Command Prompt (batch file).
I know how to start a program when the batch file is running:
#echo off
Start [adress of application]
I believe it's not possible, because when you terminate the batch (by pressing the red 'X' on top), it will end imediately without doing anything.
But you can Read the user input, and if the user writes "Exit", it will run a program and close. I think you don't want that, but If you, then look at this: In Windows cmd, how do I prompt for user input and use the result in another command?
I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.
I would like to run program from windows console(cmd.exe) and close console after that. now when i do it the console stays opened as long as the program is running.
I would like it to start program, close console and use program.
I double click the batch
#c:\somedir\app.exe
Use start
START "title" [/Dpath] [options] "command" [parameters]
Example: start /B "Test" program.exe
http://ss64.com/nt/start.html
I'm not running Windows right now so I can't try it out, but have you looked into start? You can find more information here.
Another option might be call.
I have creted a batch file that I want to run the SetupCodeGroup.exe
When I double click the batch file it doesnt run the exe file. A command prompt opens up but it doesnt run the file. Can someone tell what I missed or what I am missing
C:\Users\raw008\Desktop\Critcare\SetupCodeGroup.exe
type pause on the next line and check whether this executable comes on the command prompt .
try
start C:\Users\raw008\Desktop\Critcare\SetupCodeGroup.exe
moreover
start /d "path_to_file_directory" program.exe
is the complete line to execute program and console will not wait to program to exit .
I wonder if this exe requires administrative privileges. Try right-clicking the batch file and running it as administrator.
#echo off
start C:\Users\raw008\Desktop\Critcare\SetupCodeGroup.exe
echo Done
pause
try that if that dose not work then your computers privleges are messed up, i ran into the same problem on my cousins computer.
when i run a .BAT file, it displays a message but immediately closes the window
how i can i force it to keep the window open so that i can see the message reeturned?
One way is to conclude the batch file with the
PAUSE
command.
You can also wrap it another batch file that calls the original and then pauses:
FOO.BAT
PAUSE
This also works for read-only batch files and compiled executables.
An alternative is to execute this bat file from an already opened command prompt (shortcut: Windows Key + R, type cmd, press ENTER)