Update - Scroll down to the 5th post to see how it can be used to cleanly exit from batch processing, regardless how many CALLs have been issued.
Here is a very simple TEST.BAT:
Code: Select all
@echo off
:loop
echo before
cmd /c exit -1073741510
echo after
echo(
goto loop
The Ctrl-C exit code returned from CMD /C is interpreted the same as if someone pressed the Ctrl-C key. The same effect is achieved with a return value of 3221225786.
Here is sample output with 2 iterations, first entering "n", then "y":
Code: Select all
C:\test>test
before
^CTerminate batch job (Y/N)? n
after
before
^CTerminate batch job (Y/N)? y
C:\test>
The ^C that displays can be hidden by redirecting stderr to nul. It is not the actual Ctrl-C character.
Code: Select all
@echo off
:loop
echo before
cmd /c exit -1073741510 2>nul
echo after
echo(
goto loop
sample output:
Code: Select all
C:\test>test
before
Terminate batch job (Y/N)? n
after
before
Terminate batch job (Y/N)? y
C:\test>
The ^C display is not the actual Ctrl-C character. It is actually a caret, followed by C, which is displayed on stderr when CMD.EXE detects Ctrl-C. That can be seen by redirecting stderr to stdout, and capturing the value with FOR /F.
Code: Select all
@echo off
setlocal enableDelayedExpansion
for /f %%C in ('cmd /c exit -1073741510 2^>^&1') do (
set "str=%%C"
echo str[0]=!str:~0,1!
echo str[1]=!str:~1,1!
)
output:
Code: Select all
str[0]=^
str[1]=C
Dave Benham