Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows batch scripting: catch user reaction to "timeout" command

I know that by using the "timeout" command, I can wait for specified amount of time; but my question is that what if this would be an automatic operation that could be prevented by user? I mean suppose that I wanted to do the operation A but by using the "timeout" command I wait if user wants to cancel this operation or not; for example during this waiting process if user pressed the Enter key then batch script execute something else(not operation A);enter image description here

like image 994
wiki Avatar asked Jan 29 '14 15:01

wiki


People also ask

How do I use command prompt for wait?

Syntax TIMEOUT delay [/nobreak] Key delay Delay in seconds (between -1 and 100000) to wait before continuing. The value -1 causes the computer to wait indefinitely for a keystroke (like the PAUSE command) /nobreak Ignore user key strokes.

Is there a wait command in batch?

The Sleep/Wait Command is a very useful command that is used to pause for a set amount of time while a batch script is being executed. To put it another way, this command makes it easier to run a command for a set amount of time.

How do you check if a command is executed successfully in a batch file?

To test for a specific ERRORLEVEL, use an IF command with the %ERRORLEVEL% variable. n.b. Some errors may return a negative number. If the batch file is being executed as a scheduled task, then exiting with an error code will be logged as a failed task. You can monitor the event log to discover those failures.


1 Answers

timeout is just to induce a delay before an action, not to let the user choose something. I f you still want to use it, you need to ask the user to press Control-C to break before the timeout command, and catch the ERRORLEVEL after that.

Else choice is your friend here:

C:\Users\mm>CHOICE /T 10 /C yYcC /CS /D y  /M "Press y to validate, c to Cancel. You have 10 sec delay [Default y]:"
Press y to validate, c to Cancel. You have 10 sec delay [Default y]: [y,Y,c,C]?y

And you can test returned value with ERRORLEVEL. Here, I pressed nothing, so y get selected (default value via /D) and ERRORLEVEL is 1 (1st option). The countdown is not displayed though.

choice.exe reference at ss64.com

like image 57
Mat M Avatar answered Sep 28 '22 08:09

Mat M