[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11581
  • Last Modified:

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

0
noodleNT
Asked:
noodleNT
  • 6
  • 3
  • 2
1 Solution
 
t0t0Commented:
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.
 
0
 
oBdACommented:
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

0
 
oBdACommented:
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.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
t0t0Commented:
You can download a compatitble CHOICE here:

http://my.vector.co.jp/servlet/System.FileDownload/download/http/0/349380/pack/win95/util/bat/dkclonesup.zip

Unzip the CHOICE command in the NT folder.

Then, copy-and-paste the following code into notepad and save it as MENU.BAT in the same folder as the CHOICE.EXE command. Once done, fire up a DOS command box, CD yourself to the folder where you've saved these files and run the batch file by typing MENU.


:: -------------------------------------------------------
:: MENU
:: Written by (t0t0) Paul Tomasi - May 2009
:: -------------------------------------------------------
@echo off
:main
   setlocal enabledelayedexpansion
   call :set_screen_size
   call :display_menu
   call :get_option
   call :reset_screen_size
   
   echo.
   echo Option %option%
   echo.
exit /b

:: -------------------------------------------------------
:: SET SCREEN SIZE
:: -------------------------------------------------------
:set_screen_size
   mode|findstr "Lines">mode.txt
   mode|findstr "Columns">>mode.txt
   mode con: cols=80 lines=50
exit /b 0

:: -------------------------------------------------------
:: RESET SCREEN SIZE
:: -------------------------------------------------------
:reset_screen_size
   for /f "tokens=1-2 delims= " %%a in (mode.txt) do (
      if %%a==Lines: set Lines=%%b
      if %%a==Columns: set Columns=%%b
   )
   mode con: cols=%Columns% lines=%Lines%
   del mode.txt 2>nul
exit /b 0

:: -------------------------------------------------------
:: DISPLAY MENU
:: -------------------------------------------------------
:display_menu
   call :write 10,  05, "Please select which application you wish to run"
   call :write 10,  07, "1: Office2003 (Default)"
   call :write 10,  08, "2: Office2007"
   call :write 10,  0A, "Enter choice: "
exit /b 0

:: -------------------------------------------------------
:: GET OPTION
:: -------------------------------------------------------
:get_option
   set count=15
   set option=1
   set default=0
:loop
   call :write 10, 0C, "Time remaining:%count% "
   call :write 10, 0A
   
   choice\nt\choice /c:012 /n /t:%default%,1 Enter choice:
   if not %errorlevel% EQU 1 (
      set /a option=%errorlevel%-1
      set count=0
   ) else (
      set /a count-=1
      if %count% EQU 1 set default=1
   )  
   if !count! GTR 0 goto :loop
exit /b 0

:: -------------------------------------------------------
:: WRITE
:: -------------------------------------------------------
:write
   rem Written by t0t0 (Paul Tomasi) May 2009
    >script echo a
   >>script echo push ax
   >>script echo mov  ah, 02
   >>script echo mov  bh, 00
   >>script echo mov  dh, %2
   >>script echo mov  dl, %1
   >>script echo int  10
   >>script echo pop  ax
   >>script echo ret
   >>script echo.
   >>script echo rcx
   >>script echo 0d
   >>script echo n gotoxy.com
   >>script echo w
   >>script echo q
   debug<script>nul
   gotoxy
   if not "%~3"=="" echo %~3
   del gotoxy.com 1>nul 2>nul
exit /b
0
 
noodleNTAuthor Commented:
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.
0
 
t0t0Commented:
Glad we got there in the end.

Have a good day.
0
 
noodleNTAuthor Commented:
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

0
 
t0t0Commented:
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.


 

0
 
t0t0Commented:
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.



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

Thanks again for the help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now