Mark Gamez
Mark Gamez

Reputation: 13

Command to auto-exit a batch if left idle?

I was wondering if there was a command to automatically exit a batch file if it is left alone for a certain number of seconds.

I made a little program similar to the one found here. Most of the coding I used is displayed on the page, but it basically asks you what website you want to visit, and selecting one of the options opens a browser window with the desired page. however, after selecting one of the listed sites, the program displays the options to either exit or return to the top. This is where I usually forget about it until I close whatever I was looking at, and the batch is still open in the background.

So is there anyway to set a auto-exit timer without it interrupting the user, and without restricting the ability to go back and select another option?

Thanks!

Upvotes: 1

Views: 1098

Answers (2)

Honguito98
Honguito98

Reputation: 694

You can use choice command, instead set /p

Where z is an automatic option, you can use another letter.

/D is the default option if time is passed. /T is the time to wait (this case, 5 seconds).

choice /n /c:zbe /T 5 /D x /M "Make your selection"

Then use it on your code:

choice /n /c:xbe /T 5 /D x  /M "Make your selection"
if errorlevel 1 exit
if errorlevel 2 goto :option_b
if errorlevel 3 goto :option_e

More info type in cmd: choice /?

Upvotes: 2

Fabrice T
Fabrice T

Reputation: 663

Please take a look here

set /p udefine=

this line is waiting for b or e

Since you only want to exit the batch file after selecting one website, you can just wipe these lines

echo Type [e] to exit or [b] to go back and select another site.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************

and also this line (before the last one)

pause

Upvotes: 1

Related Questions