Link to home
Start Free TrialLog in
Avatar of noodleNT
noodleNT

asked on

Batch Menu with 15 second countdown timer

Is there a way in a batch file to have the user input a variable without using the SET /P

I would like a menu count down for 15 seconds then automatically pick the default option at the end of the set timer. The example would be something like this below.

I was hoping to do this without CHOICE.EXE if possible. This is going to run in WinPE as part of our computer build process.
@ECHO OFF
setLocal EnableDelayedExpansion
 
set /a seconds=15
 
:gotsecs
Set OriginalTime=%time%
set /a counter= %seconds%
if /i %counter% equ 0 goto finish
set checktime= %time:~7,1%
goto timecheck
 
 
:Timecheck
if /i %checktime% equ %time:~7,1% (goto timecheck)
Set /a counter= %counter% - 1
set /a checktime= %time:~7,1%
goto display
 
 
:display
Set /a secondsremaining= %counter%
 
CLS
Echo Seconds Remaining:  !secondsremaining!
if /i %counter% equ 0 (goto Office2003)
ECHO Install which version of Office Version:
ECHO 1] Office 2003 (DEFAULT)
ECHO 2] Office 2007
Set /p userinput=Pick Version: 
if %userinput% equ 1 (goto Office2003)
if %userinput% equ 2 (goto Office2007)
goto Timecheck
 
:Office2003
ECHO Installing Office 2003
goto Finish
 
:Office2007
ECHO Installing Office 2007
goto Finish
 
 
:finish
Set Endtime=%time%
echo Time Waited: 0:0:%Seconds%
echo Start Time: %OriginalTime:~0,8%
Echo End Time: %Endtime:~0,8%
 
 
%wtd%
exit /b

Open in new window

Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

So, you don't want to use SET /P and you don't want to use CHOICE either.... Hmmm.....

I thought CHOICE had a /T switch allowing it to pause for a given time and should there be no user input then CHOICE could be set to accept a default value.
 
Avatar of oBdA
oBdA

Try the script below; it will open the input in a separate window and kill this window if there's no input after the timeout.
You can add your own code in the marked sections, and you can of course customize the appearence of the input window (InputHeight, InputWidth, InputColor; enter "color /?" to get a list of the possible colors).
@echo off
setlocal
set InputResult=%Temp%\%~n0-Input.tmp
:: *** Title of the window with the input prompt; MUST BE UNIQUE!
set InputTitle=## Input ##
if /i "%~1"=="/input" goto LabelInputWindow
 
:: *** Enter custom code here ...
 
Echo Please enter your choice in the popup window "%InputTitle%".
call :Input 15 "0 0"
echo Your choice was: %Input%
 
:: *** Enter custom code here ...
 
 
:: ---- Begin creating input window and waiting for a result ----
:: *** %1: timeout, %2: default value to return
goto :eof
:Input
set InputTimeout=%~1
set InputDefault=%~2
if exist "%InputResult%" del "%InputResult%"
start "" "%ComSpec%" /c ""%~f0" /input %InputTimeout% "%InputDefault%""
set /a InputWait = 0
echo Waiting %InputTimeout% seconds for your input ...
:LabelInputLoop
  sleep 1
  set /a InputWait += 1
  if exist "%InputResult%" goto LabelInputContinue
  tasklist /fi "WINDOWTITLE eq %InputTitle%" 2>&1 | find "PID" >NUL || goto LabelInputDefault
if %InputWait% LSS %InputTimeout% goto LabelInputLoop
taskkill /fi "WINDOWTITLE eq %InputTitle%" >NUL 2>&1
:LabelInputDefault
>"%InputResult%" echo %InputDefault%
:LabelInputContinue
for /f "delims=" %%a in ('type "%InputResult%"') do set Input=%%a
del "%InputResult%"
goto :eof
:: ---- End creating input window and waiting for a result ----
 
:: ------------------------------------------------------------
:: ---- Begin code for input window
goto :eof
:LabelInputWindow
set InputHeight=10
set InputWidth=40
set InputColor=F0
title %InputTitle%
mode CON: COLS=%InputWidth% LINES=%InputHeight%
color %InputColor%
set InputTimeout=%~2
set InputDefault=%~3
echo Default choice is %InputDefault%
set /p Input=Please choose: 
if not "%Input%"=="" >"%InputResult%" echo %Input%
goto :eof
:: ---- End code for input window

Open in new window

Forgot to mention: the script uses sleep.exe, which is part of the Resource Kit Tools (http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en).
If you can't provide sleep.exe, but have the network installed at this time, replace "sleep 1" with
ping -n 1 -w 500 0.0.0.1 >NUL
You can experiment with the -w; the above values usually end up with about 1 second delay per ping.
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of noodleNT

ASKER

Perfect t0t0!

I didn't think Choice could run in winPE so thats why I didn't want to use it. Since the NT version does work I will just use that instead.
Glad we got there in the end.

Have a good day.
I simplified the code but it works!


@ECHO OFF
setLocal EnableDelayedExpansion
 
set /a seconds=15
 
:gotsecs
Set OriginalTime=%time%
set /a counter= %seconds%
if /i %counter% equ 0 goto finish
set checktime= %time:~7,1%
goto timecheck
 
 
:Timecheck
if /i %checktime% equ %time:~7,1% (goto timecheck)
Set /a counter= %counter% - 1
set /a checktime= %time:~7,1%
goto display
 
 
:display
Set /a secondsremaining= %counter%
 
CLS
Echo Seconds Remaining:  !secondsremaining!
if /i %counter% equ 0 (goto Office2003)
ECHO Install which version of Office Version:
ECHO 1] Office 2003 (DEFAULT)
ECHO 2] Office 2007
Choice /C:012 /N /T:0,1 Pick Version:
if %errorlevel% equ 1 (goto Timecheck)
if %errorlevel% equ 2 (goto Office2003)
if %errorlevel% equ 3 (goto Office2007)
 
:Office2003
ECHO Installing Office 2003
goto Finish
 
:Office2007
ECHO Installing Office 2007
goto Finish
 
:finish
Set Endtime=%time%
set /a waited=%Seconds%-%counter%
echo Time Waited: 0:0:%waited%
echo Start Time: %OriginalTime:~0,8%
Echo End Time: %Endtime:~0,8%
 
 
%wtd%
exit /b

Open in new window

NoodleNT

I'm referring to my code above, I just noticed the line:

   choice\nt\choice /c:012 /n /t:%default%,1 Enter choice:

should actually be:

   choice /c:012 /n /t:%default%,1 Enter choice:

as the other bits were part of my test code but you probably figured this out anyway.


 

NoodleNT

I just ran the code you modified and it does not do the countdown on the screen - which is why the /T option in choice is only set to 1 second. You're missing a loop which loops down 15 seconds each time re-displaying the time remaining.

Did you not run my code? If you run my code, you'll see the full effect of what I aimed to achieve. If you study my GET OPTION section of code carefully, you'll realise why I set defualt=0, option=1, why option becomes %errorlevel%-1 and the significance of testing whether %count% equals 1. It's not immeiately apparent but it provides an interruptible countdown. This is why I had to set default to 1 during the last second.

I take it you weren't phased by the WRITE routine. Of course, I could have written the whole thing using just 5 lines of code had you not wanted the countdown displayed on the screen and without the fancy text  positioning.

Just a tip for you though... If you're not using the WRITE function to position text on the screen then at least us white space when ECHOing text to the display as in:

   ECHO.
   ECHO.
   ECHO.
   ECHO.
   ECHO.
   ECHO                    [1] Office2007
   ECHO                    [2] Office2003
   ECHO.
   ECHO                    Please choose your application:
   etc...

Anyway, best of luck with your project.



Apologies.... your code DOES count down.... I fell victim of my own bad path to CHOICE.EXE.
Yep... had that same problem with your code the first time too. :)

Thanks again for the help.