close cmd window after executable
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tyler Murray
le 27 Sep 2016
Commenté : Walter Roberson
le 25 Nov 2016
I am running an external .bat file through matlab. The code is as follows:
fileName = uigetfile('*.*');
cmd = sprintf(program.bat "%s" &', fileName);
system(cmd);
The user gets to pick which file is run through the .bat file and then it runs. The problem I am trying to solve, which I am not even sure it's possible, is to close the cmd window after it is done autonomously. The reason this is difficult is because after the program is done running, if you press enter, it prompts excel to open and displays the results in excel which I am trying to avoid. Thus, I don't think I can write a simple exit command. I also do not have access to the batch file to rewrite that code.
1 commentaire
Réponse acceptée
Walter Roberson
le 27 Sep 2016
There are a few approaches:
- Java Robot Class to position on top of the command window and send the key/mouse sequence to close it;
- call taskmanager to kill the process
- instead of using system() use one of the activex / DCOM methods to create a process and send it a command line; you can then also kill off the process. I know this is possible, and I know someone showed how to create a process not long ago, but I am having difficulty finding the appropriate sequence at the moment.
3 commentaires
Walter Roberson
le 29 Sep 2016
https://www.mathworks.com/matlabcentral/answers/100545-how-can-i-programmatically-control-mouse-motion-and-clicks-with-matlab shows an example of move movement.
Finding the location of the command window might be tricky. You need to find a HWMD .
The contribution http://www.mathworks.com/matlabcentral/fileexchange/3407-showwindow/content/showwindow.m might help
Plus de réponses (1)
Mikhail
le 24 Nov 2016
Much easier:
fileName = uigetfile('*.*');
cmd = sprintf('start program.bat "%s"', fileName);
system(cmd);
7 commentaires
Mikhail
le 25 Nov 2016
It the process doesn't exit by itself, wrap it in another bat file like that:
my.bat:
REM exit after 5 seconds
@ping 127.0.0.1 -n 5 -w 1000 > nul
my_wrapper.bat:
call my.bat
exit
Then:
system('start my_wrapper.bat')
That is all to it.
Walter Roberson
le 25 Nov 2016
Now try again with a process that does not exit by itself -- for example a ping that would go on forever until it is interrupted.
The user wants to be able to start an asynchronous process of indefinite duration and kill it at some later time before it has finished. The solutions I gave are various methods of killing asynchronous processes.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!