Problem : 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.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: |
@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 |
Solution : Batch Menu with 15 second countdown timer
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