In cmd, when we press Ctrl+C we get the target application terminated but if the target application is called from a batch file, we get this "Terminate batch job (Y/N)" confirmation. I can never remember an instance where I chose not to terminate the batch job. How can we skip this confirmation?

link|improve this question

Did you ever try sgmoore's answer? superuser.com/questions/35698/… – Arjan Sep 27 '09 at 10:40
feedback

9 Answers

up vote 8 down vote accepted

AFAIK you can't as this behavior is by design and controlled by the command interpreter. There is no method of "mapping" or even "intercepting" this unless you de-compile and recompile the interpreter directly.

link|improve this answer
That's disappointing :( – artknish Sep 4 '09 at 12:42
Yes it is disappointing. What’s even more disappointing is that the BREAK command, which by default does nothing under XP, could have been used to toggle the prompt… – Synetech Dec 24 '09 at 19:12
Though it might be true one cannot make the interpreter behave differently, using start like sgmoore suggested at superuser.com/questions/35698/… seems a perfectly fine workaround to me. – Arjan Jan 6 '10 at 16:44
feedback

If you don't need to do anything in the batch file after your application finishes normally, then using the start command ensures that the batch file is already finished by the time you press Ctrl-C. And hence the message will not appear.

For example:

@echo off

set my_command=ping.exe
set my_params=-t www.google.com

echo Command to be executed by 'start': %my_command% %my_params%

:: When NOT using /B or /WAIT then this will create a new window, while
:: execution of this very batch file will continue in the current window:

start %my_command% %my_params%

echo.
echo This line will be executed BEFORE 'start' is even finished. So, this
echo batch file will complete BEFORE one presses Ctrl-C in the other window.
echo.

:: Just for testing use 'pause' to show "Press any key to continue", to see
:: the output of the 'echo' commands. Be sure to press Ctrl-C in the window
:: that runs the 'ping' command (not in this very window). Or simply remove
:: the next line when confused:

pause

(Tested on Windows XP.)

link|improve this answer
I don't know if start would indeed help, but it sounds plausible to me. So I wonder if the downvote implies that this would NOT work? Or maybe whoever downvoted does not know Windows' start (or especially start /wait and start /b) command? See microsoft.com/resources/documentation/windows/xp/all/proddocs/… then. Please explain the downvote? – Arjan Sep 4 '09 at 18:48
start won't work. Whenever cmd is running a batch file, tapping Ctrl+C will cause the currently running process to terminate and show the question. – Joey Sep 5 '09 at 8:54
All I can say, is that it works for me and the significant point is that the cmd is NOT running a batch file (because it has finished). – sgmoore Sep 5 '09 at 10:15
But could one still terminate the application then? So: can Ctrl-C be used to terminate the application that was invoked using start? (Still sounds plausible to me...) – Arjan Sep 7 '09 at 12:21
Whether Ctrl-C will terminate the application is independent of whether or not the application is run from a batch file. For example, if (in XP) you start c:\windows\system32\edit, it will not respond to the Ctrl-C regardless of how it is run. However defrag does respond to the ctrl-C. So the simpliest way to test this is to create a batch file that simply says start defrag c: – sgmoore Sep 7 '09 at 16:00
show 4 more comments
feedback

Press Ctrl+C twice.

link|improve this answer
This won't work but rather repeats the prompt. – Joey Sep 17 '09 at 5:25
Are you sure? I just tried it in an XP command prompt and it worked. I’ll check in DOS 7.11 later, but I’m pretty sure that Enter causes it to repeat the prompt, rather than Ctrl-C. – Synetech Dec 24 '09 at 19:10
I tried it in PS 2.0 on Win7 and it worked. However I have seen the repeating prompt too in the same env. It's a mystery. Still, +1. – jcollum Nov 22 '11 at 19:06
feedback

I've been fighting with this desire to avoid the "Terminate batch job" prompt for a little while. My latest epiphany is a bit of a slight-of-hand (or DOS box), by replacing one instance of cmd.exe with another. This is accomplished by executing the command/program via start cmd /k followed immediately by exit in the .BAT file. The original DOS box disappears and the replacement DOS box can be stopped cleanly via Ctrl-C.

Consider the following example of a traceroute that can be interrupted by Ctrl+C, or allowed to complete, returning the user to the C:\> prompt:

@Echo Off
REM
set timeout=100
if not "%2"=="" set timeout=%2
REM
start cmd /k tracert -w %timeout% %1
exit

The environment substitution of a new command interpreter may not be for everyone, but to the naked eye, looks and works well for me.

link|improve this answer
feedback

See this Stack Overflow question.

However, patching cmd.exe is not something I would do for that.

link|improve this answer
feedback

In my case it was the ping.bat file that was right in my user directory (C:\Users\ in Vista or C:\Documents and Settings\ in XP) that was holding the batch job indeterminately.

This batch file was executed whenever I ran ping from the command prompt where the current directory is my user directory. Ping-ing from the Run window, or from other user's directory was running well.

Removed the file from my user dir and the problem was resolved!

link|improve this answer
feedback

Start works, but now the window opened by the batch file is changed from the options I had and the "properties" are disabled (won't respond).

link|improve this answer
feedback

Simply redirect the batch stdin to null by adding >nul to the end of the line.

link|improve this answer
I think you may have meant <nul (as the >nul sends all stdout to null). – Brian Phillips Dec 7 '11 at 13:54
feedback

At the end of your script, just add the following command:
echo
This will not "damage" the behavior of your script, and it seems to prevent CDM to ask if you want to termininate the batch.

link|improve this answer
Didn't work for me. – Helgi Apr 18 at 18:47
feedback

protected by studiohack May 16 '11 at 18:41

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.