close command / dos window

조회 수: 59 (최근 30일)
Tyler Murray
Tyler Murray 2016년 9월 6일
답변: Kevin Rawson 2019년 4월 7일
I am running a .bat file using either system() or dos() which opens a cmd window. I would like to close the cmd window when it is done. I have searched on here but haven't found a solution that works.
  댓글 수: 1
dpb
dpb 2017년 6월 5일
Show the command you're executing...

댓글을 달려면 로그인하십시오.

답변 (4개)

Kevin Rawson
Kevin Rawson 2019년 4월 7일
Simplest way without modifying your original batch file would be to add an exit command to your system call.
system('yourScript.bat && exit &')
This will complete execution of "yourScript.bat" in a new DOS command window, and then close the spawned window.

dpb
dpb 2017년 6월 5일
If you must use the trailing '&' to dispatch the command window to the background while the Matlab system continues, then you don't have control of that window with just the supplied system. Others have shown a way to kill a process; the alternate would be to add an EXIT command as the last entry in the .bat file to close the session when the routine terminates.

Nagesh
Nagesh 2017년 1월 11일
편집: Walter Roberson 2017년 1월 12일
let's say you want to kill both your progam (yourprogram.exe) and the cmd window that gets opened with the system command, you can use the following command
[status1]=system('"C:\Windows\System32\taskkill.exe" /F /im yourprogram.exe /im cmd.exe &');
This will kill 'yourprogram' and also closes 'cmd' window
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 1월 12일
That looks to me as if it would close all of the command windows. You might plausibly have another one that you did not want closed.
dpb
dpb 2018년 12월 27일
편집: dpb 2018년 12월 27일
The other issue is that passing off TASKLIST or TASKKILL to the system via dos() spawns a new CMD.EXE besides any other that may be open -- w/o some other variable like the WINDOWTITLE if it were set uniquely when the process was started, you can't tell "who's who in the zoo" as to which it is, specifically, to kill.
(I was just thinking about the same problem as was spawning a bunch of editor windows to do some cleanup on Excel spreadsheet comments outside the sorry edit function it supplies for the purpose internally which is got me here now...)
ADDENDUM
I'd done this ages ago and had forgotten all I once knew...ran into one kicker I can't presently seemingly get around -- tasklist /fi "WINDOWTITLE eq title string" never returns a match, even if copy and paste what is returned by the /V option for the specific window so can't find a unique CMD.EXE amongst others that way, either. :(
ADDENDUM SECOND
Albeit one could return the list of all and parse it separately to find the PID, I suppose...
ADDENDUM THIRD
The simplest answer (just tested) is to do as suggested in above Answer; just add exit to the batch file to close the shell when the app exits back to command line. Done!

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 9월 7일

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by