Catching User Input on "Terminate batch job (Y/N)?" prompt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Catching User Input on "Terminate batch job (Y/N)?" prompt

#1 Post by sambul35 » 16 Feb 2016 11:08

Hello guys,

I was looking for a simple way to catch user input after Crtl+C is pressed and Terminate batch job (Y/N)? prompt is shown in a Win 10 Cmd window, but amazingly can't find any on the web. For example, depending on user input:

Code: Select all

if "y" command1 else if "n" command2

What variable Cmd sets for user input in this case? Any suggestions how to catch?

Related, I found a suggestion how to ignore Ctrl+C altogether here, but could you explain what variable %~1 means in this code, and what the code does:

Code: Select all

@ECHO OFF
IF "%~1"=="–FIX_CTRL_C" (
 SHIFT
 ) ELSE (
 CALL <NUL %0 –FIX_CTRL_C %*
 GOTO :EOF
 )


Another question... When

Code: Select all

shutdown /l /f
is processed in a batch, the system closes all programs and logs off a user abruptly, while if the user selects Sign Out in Shutdown Options manually, the system will exit all running programs gracefully offering to Save unfinished work, and then logoff the user. Is there a way to logoff a user gracefully from a batch without knowing what programs are currently running? One way I can think of is showing Msg prompt notifying of incoming Logoff and asking a user to finish all work. Any other automated ways?
Last edited by sambul35 on 17 Feb 2016 12:26, edited 3 times in total.

kwsiebert
Posts: 43
Joined: 20 Jan 2016 15:46

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#2 Post by kwsiebert » 16 Feb 2016 15:44

The redirection technique you linked doesn't actually ignore Ctrl+C completely. In Win7 at least, the prompt still appears, but is moved past. By rapidly pressing Ctrl+C as fast as I could, I was able to bypass that and get a prompt that allowed me to cancel my script.

The code snippet you posted is a just a technique to have a batch script restart itself using the previously described technique. The script inserts –FIX_CTRL_C as the first argument when it restarts. Then, if –FIX_CTRL_C is present, it continues on with the rest of the script instead of restarting.

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

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#3 Post by foxidrive » 16 Feb 2016 15:54

That script has wrong quote characters though, as it is posted.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#4 Post by sambul35 » 16 Feb 2016 16:12

Such quotes were in original code, I corrected them now. But... it doesn't substitute the question: "How to process Y/N user input on Ctrl+C in a batch"? What variable Cmd uses for that input? :wink:

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#5 Post by sambul35 » 17 Feb 2016 12:36

kwsiebert wrote:The code snippet you posted is a just a technique to have a batch script restart itself using the previously described technique.


So this is not a function that needs to be called from a certain place in a batch? Its actually an example of starting your own batch script, so one needs to substitute %0 with its own batch name, and %* with its batch args - correct? If not, where this snippet must be placed in a batch code?

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

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#6 Post by aGerman » 17 Feb 2016 13:08

I executed an example

Code: Select all

@echo off &setlocal

if "%~1" neq "–FIX_CTRL_C" (
  call <nul "%~fs0" –FIX_CTRL_C %*
  goto :eof
)
shift

:loop
echo bla
goto loop

But the code behaves the same with and without the IF statement when Ctrl+C was pressed. No idea for what purpose it was actually written.

"How to process Y/N user input on Ctrl+C in a batch"?

As you know Ctrl+C suspends the script execution. What part of a stopped code should ever catch the user input?

Regards
aGerman

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#7 Post by sambul35 » 17 Feb 2016 14:39

Good sense of humor, but... I can catch Ctrl+C click many times when entered at user prompt with

Code: Select all

IF %errorlevel% equ 1 goto :keepgoing


So why its so surprising to be able to catch Y/N user input after Ctrl+C? Btw, Cmd continues the code execution for sure if a user enters N. That would be something to catch even N this way, namely if code continues to execute. :) And before Cmd processes Y, the batch is still paused but not stopped, so it might be possible to catch Y as well as some "error".
Last edited by sambul35 on 17 Feb 2016 15:41, edited 2 times in total.

kwsiebert
Posts: 43
Joined: 20 Jan 2016 15:46

Re: Catching User Input on "Terminate batch job (Y/N)?" prompt

#8 Post by kwsiebert » 17 Feb 2016 15:34

sambul35 wrote:So this is not a function that needs to be called from a certain place in a batch? Its actually an example of starting your own batch script, so one needs to substitute %0 with its own batch name, and %* with its batch args - correct? If not, where this snippet must be placed in a batch code?

%0 is the batch file's own name, and %* is the full list of command line arguments, so nothing needs to be substituted. That code simply goes at the top of any script without changes.

aGerman wrote:I executed an example

But the code behaves the same with and without the IF statement when Ctrl+C was pressed. No idea for what purpose it was actually written.

I get different behavior with and without the IF statement when testing on English Windows 7 Pro. It displays the batch termination prompt, but does not actually stop the batch or accept input.

Post Reply