Computer Hope

Microsoft => Microsoft DOS => Topic started by: blanktheman on January 16, 2018, 06:17:35 AM

Title: Batch file to autoselect option after 1 minute
Post by: blanktheman on January 16, 2018, 06:17:35 AM
Hi all. Many years ago I had a similar question and it was answered. I can't find my old profile or the old post. I have code from another person in the forums and now I want to add a line so that it auto selects "N" after 60 seconds. I remember the command being so easy.

Code: [Select]
:choice
set /P c=Are you sure you want to continue[Y/N]?
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice

I have tried the choice.exe but I can't seem to get choice.exe working. Please help.
Title: Re: Batch file to autoselect option after 1 minute
Post by: Squashman on January 16, 2018, 07:51:25 AM
Code: [Select]
choice /C YN /D N /T 6 /M "Are you sure you want to continue?"
Title: Re: Batch file to autoselect option after 1 minute
Post by: blanktheman on January 16, 2018, 09:40:33 AM
Thanks for the response, how do i code a response? I press Y or N and the bat file closes. I've put pause in so the batfile should stop. I currently have.

Code: [Select]
if /D "y" goto :imhere
if /D "n" goto :imnothere
Title: Re: Batch file to autoselect option after 1 minute
Post by: BC_Programmer on January 16, 2018, 11:32:19 AM
from CHOICE /?

Quote
   The ERRORLEVEL environment variable is set to the index of the
   key that was selected from the set of choices. The first choice
   listed returns a value of 1, the second a value of 2, and so on.
   If the user presses a key that is not a valid choice, the tool
   sounds a warning beep. If tool detects an error condition,
   it returns an ERRORLEVEL value of 255. If the user presses
   CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
   of 0. When you use ERRORLEVEL parameters in a batch program, list
   them in decreasing order.
Title: Re: Batch file to autoselect option after 1 minute
Post by: Salmon Trout on January 16, 2018, 12:36:32 PM
Quote
if /D
I don't recognise the /D switch for IF.
Title: Re: Batch file to autoselect option after 1 minute
Post by: Squashman on January 16, 2018, 02:09:02 PM
from CHOICE /?
Reading is FUN!
Title: Re: Batch file to autoselect option after 1 minute
Post by: blanktheman on January 16, 2018, 10:39:04 PM
I don't recognise the /D switch for IF.

Reading is FUN!

thanks guys but I think you missing the point. My first post, the code works fine. I don't really want to use choice.exe but if I have to use it then how do i make my bat file work? Squashman gave me a code using choice.exe and it works. Now where do I go from there? I've even tried taking out the top line of my code and inserting squashman's code and using the /I switch but it doesn't work because obviously it can't find c.

There is a way to not use choice.exe and I would prefer that way but if i have to use choice.exe, how do I select a choice so the bat file runs the commands after Y and N?
Title: Re: Batch file to autoselect option after 1 minute
Post by: blanktheman on January 16, 2018, 11:00:44 PM
from CHOICE /?

Of course I haven't forgotten about you. How do I program the bat file to carry on and select y or n? When I press Y or N the bat file closes. Can you show me the code?
Title: Re: Batch file to autoselect option after 1 minute
Post by: blanktheman on January 17, 2018, 12:47:45 AM
I finally found the correct solution on google after a long time of searching. Here is my complete bat file. Please note the Error as proposed by bc_programmer.

Code: [Select]
@ECHO OFF

:choice
choice /C YN /D N /T 60 /M "Are you sure you want to continue?"
IF errorlevel 2 goto :somewhere_else
IF errorlevel 1 goto :somewhere
pause
goto :choice