Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to autoselect option after 1 minute  (Read 927 times)

0 Members and 1 Guest are viewing this topic.

blanktheman

    Topic Starter


    Rookie
  • Technician
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Batch file to autoselect option after 1 minute
« 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.

Squashman



    Specialist
  • Thanked: 121
  • Experience: Experienced
  • OS: Other
Re: Batch file to autoselect option after 1 minute
« Reply #1 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?"

blanktheman

    Topic Starter


    Rookie
  • Technician
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file to autoselect option after 1 minute
« Reply #2 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

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1087
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 8
Re: Batch file to autoselect option after 1 minute
« Reply #3 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.
I was trying to dereference Null Pointers before it was cool.

Salmon Trout



    Genius

    Thanked: 931
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Other
Re: Batch file to autoselect option after 1 minute
« Reply #4 on: January 16, 2018, 12:36:32 PM »
Quote
if /D
I don't recognise the /D switch for IF.

Squashman



    Specialist
  • Thanked: 121
  • Experience: Experienced
  • OS: Other
Re: Batch file to autoselect option after 1 minute
« Reply #5 on: January 16, 2018, 02:09:02 PM »

blanktheman

    Topic Starter


    Rookie
  • Technician
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file to autoselect option after 1 minute
« Reply #6 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?

blanktheman

    Topic Starter


    Rookie
  • Technician
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file to autoselect option after 1 minute
« Reply #7 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?

blanktheman

    Topic Starter


    Rookie
  • Technician
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 10
Re: Batch file to autoselect option after 1 minute
« Reply #8 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