Hubert Kenobi
Hubert Kenobi

Reputation: 39

How to kill a batch program if as no action after "x" seconds?

How can I create a command for a prompt with a timer, and If I do not respond in that time, it shuts down the program. Don't know if it's possible to do it directly in a single batch, or I need to create a new batch which monitors the main program (like detecting interaction with the user and program)...

This is my batch program:

echo off
:START
echo .
echo . Chose one shortcut:
echo .
echo . 1 . 2017 Folder
echo . 2 . Design Folder
echo . 3 . MIDAS
echo . 4 . 2017 Folder
echo .
echo . R:
:: Here is the problem, here I want to detect how many seconds the user  is spending 
to answer the program, if like 10 secs, it will close automaticly.
set /P a=Type option:
If %a%==1 goto A 
If %a%==2 goto B
If %a%==3 goto C
If %a%==4 goto D


:A
echo Exit
echo Exiting
exit

:B
echo Design Folder
echo \\FAP-mmedia\MMEDIA\Mediateca_Corrente\Design
start \\FAP-mmedia\MMEDIA\Mediateca_Corrente\Design
GOTO START

:C
echo MIDAS
start \\Fap-dept\dept\RIALFA\EMFA\SDFA\06_CAVFA\Bases_de_Dados\MIDAS.mdb
GOTO START

:D
echo 2017 Folder
echo \\FAP-mmedia\MMEDIA\Mediateca_Corrente\Design\2017
start \\FAP-mmedia\MMEDIA\Mediateca_Corrente\Design\2017

GOTO A

Thank you for your help.

Now I used CHOICE: It's giving me an error... I wrote this:

CHOICE /C 1234 /T 10 /D 1 /M "Type the option:"
IF ERRORLEVEL 1 GOTO A
IF ERRORLEVEL 2 GOTO B
IF ERRORLEVEL 3 GOTO C
IF ERRORLEVEL 4 GOTO D

but it ignores the A,B,C,D groups, it just continues as reading the script without jumping to the respective Letter.

Upvotes: 0

Views: 97

Answers (2)

Compo
Compo

Reputation: 38614

To switch the order from Squashman's answer use:

CHOICE /C 1234 /T 10 /D 1 /M "Type the option:"
IF %ERRORLEVEL%==1 GOTO A
IF %ERRORLEVEL%==2 GOTO B
IF %ERRORLEVEL%==3 GOTO C
IF %ERRORLEVEL%==4 GOTO D

Upvotes: 1

Squashman
Squashman

Reputation: 14290

You need to check the ERRORLEVELS from largest to smallest.

If you read the help for the IF command you will see this.

ERRORLEVEL number Specifies a true condition if the last program run
                  returned an exit code equal to or greater than the number
                  specified.

This code works just fine for me.

@echo off
:START
CLS
CHOICE /C 1234 /T 10 /D 1 /M "Type the option:"
IF ERRORLEVEL 4 GOTO D
IF ERRORLEVEL 3 GOTO C
IF ERRORLEVEL 2 GOTO B
IF ERRORLEVEL 1 GOTO A

:A
echo in A
echo Exiting Program
pause
GOTO :EOF

:B
echo in B
pause
GOTO START

:C
echo in C
pause
goto START

:D
echo in D
pause
goto START

Upvotes: 1

Related Questions