How to exit CMD windows after calling external .exe in Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
richard1017
Posts: 3
Joined: 31 Jul 2013 10:25

How to exit CMD windows after calling external .exe in Batch

#1 Post by richard1017 » 31 Jul 2013 10:48

I wrote a batch file (cal.bat) to use an external .exe file (genm.exe), so that I can run this batch file as many times as I want in MATLAB.

The problem is the CMD windows doesn't close automatically after each run. It asks "Press <Enter> or <Space> to Exit" at the end of running genm.exe.

cal.bat
@echo off
start genm.exe main.txt
exit


I'm a newbie of batch. I guess the problem is because that the process "Press <Enter> or <Space> to Exit" at the end of running genm.exe is build in genm.exe. So the second line in cal.bat "start genm.exe main.txt" will never finish, unless I press <Enter>. And the third line "exit" will never be processed. The tenm.exe file is written by others and I can do nothing about it. So each time I run cal.bat, a CMD window opens and not close automatically. I ended up got a lot of CMD windows and bunch of genm.exe processes in task manager.

Am I right?

How can I close the CMD windows automatically after each run, without pressing <Enter> each time?

Thank you very much!

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exit CMD windows after calling external .exe in B

#2 Post by aGerman » 31 Jul 2013 13:23

Neither I know what genm.exe is nor what it belongs to. I assume the cmd process was already terminated when genm.exe reaches the end of processing. The START command runs the exe file asynchronously. That means your batch file runs genm.exe in another window and terminates immediatelly (regardless whether or not you wrote an EXIT command).

Some command line tools accept the pipelining of a character.

Code: Select all

@echo off
echo( |genm.exe main.txt

... of course without any warranty.

To make sure you don't misunderstand: This is not a batch issue! If my proposal doesn't work there is probably no chance to force quitting the exe process (unless genm.exe would have a command line switch for that purpose). If you could find any kind of documentation or manual you should read it carefully. Maybe you'll find the solution ...

Regards
aGerman

richard1017
Posts: 3
Joined: 31 Jul 2013 10:25

Re: How to exit CMD windows after calling external .exe in B

#3 Post by richard1017 » 31 Jul 2013 13:51

Thank you very much, aGerman.

Your code doesn't solve the problem and the CMD window remains.

The genm.exe is wrote in Fortran by others and I can do nothing about it.
I wrote another batch file called killtask.bat to kill the genm.exe task every time I finish running it.

Code: Select all

@echo off
taskkill /F /IM genm.exe
exit


This worked! Because I have to call the genm.exe many times in one run with different parameters, now I only have flashes of CMD windows and don't need to close all CMD windows manually.

Thank you.

-Richard

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to exit CMD windows after calling external .exe in B

#4 Post by aGerman » 31 Jul 2013 14:05

I thought about TASKKILL but since you have no chance to kill a certain genm.exe process (if you have opened it several times) and you can't determine which of them is ready for closing I dismissed my thought immediatelly :wink:

However good if it finally workes for you.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to exit CMD windows after calling external .exe in B

#5 Post by Squashman » 31 Jul 2013 14:25

Does seem risky.

richard1017
Posts: 3
Joined: 31 Jul 2013 10:25

Re: How to exit CMD windows after calling external .exe in B

#6 Post by richard1017 » 31 Jul 2013 15:56

It's surely not a perfect solution, but solve my problem.

Fortunately, I only run one genm.exe at a time. So I can run it, and pause the main process for a little bit time to make sure genm.exe is finished, and then terminate the genm.exe process. Then move on for next cycle of running genm.exe.

Thank you anyway.

-Richard

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to exit CMD windows after calling external .exe in B

#7 Post by foxidrive » 01 Aug 2013 04:38

AutoIt would probably handle this also.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to exit CMD windows after calling external .exe in B

#8 Post by Dragokas » 02 Aug 2013 12:11

Hello, richard1017.

I you want we can try other solutions like BAT/VBScript.
But I need original genm.exe file for testing.

Best wishes, Alex.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to exit CMD windows after calling external .exe in B

#9 Post by Squashman » 02 Aug 2013 13:44

As Foxidrive has already pointed out, AutoIt does a really good job of this.

But Dragokas makes a good point. You could build a hybrid batch jscript and use SENDKEYS to send the enter key to that active Window.

If your genm.exe is creating a separate cmd Window when you launch it, you could give it a distinct title when you launch it with the START command using the time or something. Then your originating batch file can launch the jscript to send the enter key to the correct genm.exe window if you are launching the script multiple times and other ones are still running.

Post Reply