Page 1 of 1

How to create a option menu

Posted: 20 Oct 2013 04:36
by Batch Artist
Hello guys again, Here I will show you how you may create an Options Menu, this is very simple.

Here is a code example I will use, than I shall explain what each command does;

Code: Select all

@ECHO off
SET /P OPT=Which option would you like?
ECHO 1) Calculator
ECHO 2) Timer
ECHO 3) Exit

IF %OPT%==1 GOTO :Calculator
IF %OPT%==2 GOTO :Timer
IF %OPT%==3 GOTO :Exit

:Calculator
REM "Code for Calculator"

:Timer
ECHO %time%

:Exit
Exit



Ok so I will explain...

You need to create a variable using "SET". Than the "/P" is saying to the file, Pause and wait for a response from the user.

The "Echo" 's allow you to see the options.

The "%OPT%" variable is what you created in the beginning, allowing you to use that as code.

The "==" basically means, if you type that digit "1/2/3" into the line that says:

Code: Select all

SET /P OPT=Which option would you like?


... Than that is the answer you decide and want to select.

The "GOTO" command, sends you to whatever point you want, i.e ":Exit".



That's how you can make an Option Menu. You can change the code to your liking, and use it for your own batch files.

Have fun and enjoy...

.Batch Artist.

Re: How to create a option menu

Posted: 20 Oct 2013 06:29
by bars143
@batch,

this is the output of my window7 32bit to your code above:

Code: Select all

Which option would you like?





its display only one-line and all the rest blank?
maybe we had different cmd.exe ?

Re: How to create a option menu

Posted: 20 Oct 2013 14:17
by trebor68
The following code make a menu.
If you will input not a number 1, 2 or 3 then will see the menu again.
Every subroutine need an endpoint.

This end point may be:
- last command in the batch
- go to another point in the batch GOTO :zzz
- go to the end of the batch :EOF
- exit the batch with or without errorlevel EXIT

Code: Select all

@ECHO off
:again
ECHO.
ECHO 1) Calculator
ECHO 2) Timer
ECHO 3) Exit
SET /P OPT=Which option would you like?

IF %OPT%==1 GOTO :Calculator
IF %OPT%==2 GOTO :Timer
IF %OPT%==3 GOTO :Exit
GOTO :again

:Calculator
REM "Code for Calculator"

EXIT
:Timer
ECHO %time%

EXIT
:Exit
Exit

Re: How to create a option menu

Posted: 21 Oct 2013 00:44
by Batch Artist
trebor68 wrote:The following code make a menu.
If you will input not a number 1, 2 or 3 then will see the menu again.
Every subroutine need an endpoint.

This end point may be:
- last command in the batch
- go to another point in the batch GOTO :zzz
- go to the end of the batch :EOF
- exit the batch with or without errorlevel EXIT

Code: Select all

@ECHO off
:again
ECHO.
ECHO 1) Calculator
ECHO 2) Timer
ECHO 3) Exit
SET /P OPT=Which option would you like?

IF %OPT%==1 GOTO :Calculator
IF %OPT%==2 GOTO :Timer
IF %OPT%==3 GOTO :Exit
GOTO :again

:Calculator
REM "Code for Calculator"

EXIT
:Timer
ECHO %time%

EXIT
:Exit
Exit


Ahhhh! Thank you for spotting that out! My terrible mistake.
That or you could have EXIT as an option if you type anything else other than a number.

Re: How to create a option menu

Posted: 21 Oct 2013 01:02
by foxidrive
Here's a neat menu that Dave wrote.

Code: Select all

:dbenham StackOverflow Oct 18 2013
@echo off
setlocal enableDelayedExpansion
set "n=0"
set "keys="
for /f "delims==" %%A in ('set $prompt 2^>nul') do set "%%A="
for %%A in (
  "1:dog        "
  "2:cat        "
  "3:hamster    "
  "4:chicken    "
  "5:cow        "
  "6:horse      "
  "7:lizard     "
  "8:snake      "
  "Q:frog       "
  "W:llama      "
  "E:sheep      "
  "R:rabbit     "
  "T:chinchilla "
  "Y:monkey     "
  "U:human      "
  "I:alien      "
  "A:godzilla   "
  "S:orc        "
  "D:medussa    "
) do (
  set /a "p=n/8,n+=1"
  for %%P in (!p!) do set "$prompt%%P=!$prompt%%P!%%~A"
  for /f "tokens=1,2 delims=: " %%B in (%%A) do (
    set "keys=!keys!%%B"
    set "code!n!=%%C"
  )
)
for /f "delims== tokens=1,2" %%A in ('set $prompt') do echo %%B
choice /c !keys! /n
for %%N in (%errorlevel%) do echo !code%%N!
pause

Re: How to create a option menu

Posted: 21 Oct 2013 01:10
by Batch Artist
foxidrive wrote:Here's a neat menu that Dave wrote.

Code: Select all

:dbenham StackOverflow Oct 18 2013
@echo off
setlocal enableDelayedExpansion
set "n=0"
set "keys="
for /f "delims==" %%A in ('set $prompt 2^>nul') do set "%%A="
for %%A in (
  "1:dog        "
  "2:cat        "
  "3:hamster    "
  "4:chicken    "
  "5:cow        "
  "6:horse      "
  "7:lizard     "
  "8:snake      "
  "Q:frog       "
  "W:llama      "
  "E:sheep      "
  "R:rabbit     "
  "T:chinchilla "
  "Y:monkey     "
  "U:human      "
  "I:alien      "
  "A:godzilla   "
  "S:orc        "
  "D:medussa    "
) do (
  set /a "p=n/8,n+=1"
  for %%P in (!p!) do set "$prompt%%P=!$prompt%%P!%%~A"
  for /f "tokens=1,2 delims=: " %%B in (%%A) do (
    set "keys=!keys!%%B"
    set "code!n!=%%C"
  )
)
for /f "delims== tokens=1,2" %%A in ('set $prompt') do echo %%B
choice /c !keys! /n
for %%N in (%errorlevel%) do echo !code%%N!
pause


Wow that's pretty sweet FoxDrive! Thanks Dave :)

That is really fascinating! Thank you for sharing that

Re: How to create a option menu

Posted: 21 Oct 2013 13:55
by trebor68
Here I have change my code.

Code: Select all

@ECHO off
:again
ECHO.
ECHO 1) Calculator
ECHO 2) Timer
ECHO 3) Exit
SET /P OPT=Which option would you like?

IF %OPT%==1 GOTO :Calculator
IF %OPT%==2 GOTO :Timer
IF %OPT%==3 GOTO :Exit
GOTO :again

:Calculator
ECHO "Code for Calculator"

GOTO :again
:Timer
ECHO "Code for Timer"
ECHO %time%

GOTO :again
:Exit
ECHO "Code for last commands before batch end"

PAUSE

Re: How to create a option menu

Posted: 22 Oct 2013 05:58
by einstein1969
modified version of dbenham:
- added contour
- auto center
- auto resize

demo with coiche commented, untested:

Code: Select all

:: dbenham StackOverflow Oct 18 2013
:: modified by einstein1969

@echo off
setlocal enableDelayedExpansion

rem set "cols=80"
set "cols=40"
set "lines=30"

:loop1

set /a "cols=cols+4"

mode con lines=%lines% cols=%cols%

set /a "cols-=3"

set "maxn="
set "n=0"
set "p=0"
set "keys="
for /f "delims==" %%A in ('set $prompt 2^>nul') do set "%%A="
for %%A in (
  "ÍÍÍÍÍÍÍÍÍÍÍÍÍ"
  "1:dog        "
  "2:cat        "
  "3:hamster    "
  "4:chicken    "
  "5:cow        "
  "6:horse      "
  "7:lizard     "
  "8:snake      "
  "Q:frog       "
  "W:llama      "
  "E:sheep      "
  "R:rabbit     "
  "T:chinchilla "
  "Y:monkey     "
  "U:human      "
  "I:alien      "
  "A:godzilla   "
  "S:orc        "
) do (
  set /a "n+=1, pn=p+1"
  if !n! equ 1 (set is=%%~A) else (
  for /f "tokens=1,2 delims= " %%P in ("!p! !pn!") do (
   set "tmp=!$prompt%%P!%%~Aº"
   if "!tmp:~%cols%!"=="" (set "$prompt%%P=!$prompt%%P!%%~Aº") else (
      if not defined maxn (set /a maxn=n-2, p+=1) else set /a "p+=1"
      set "$prompt%%Q=!$prompt%%Q!%%~Aº"
   )
  )
  for /f "tokens=1,2 delims=: " %%B in (%%A) do (
    set "keys=!keys!%%B"
    set "code!n!=%%C"
  )
  )
)

set /a "lastn=(n-1) %% maxn, i=lastn+1"
if !lastn! gtr 0 (
   set $prompt%p%=!$prompt%p%:~0,-1!Ì
   for /L %%D in (%i%,1,%maxn%) do set $prompt%p%=!$prompt%p%!!is!Ê
   set $prompt%p%=!$prompt%p%:~0,-1!¼
)

set "sp="
set "isf="
for /f "delims== tokens=1,2" %%A in ('set $prompt') do (
   set "s=º%%B"
   rem echo !s!
   if not defined sp (
      for /L %%C in (0,2,%cols%) do if "!s:~%cols%,1!"=="" (
               set "s= !s! "
               set "sp= !sp!"
            )
         if not defined sp (set "sp= "
               set "s= !s!"
         )
         rem set s=!s:~0,%cols%!
         rem echo '!s!' '!sp!'
         set isu=!is!Ë
         for /L %%D in (1,1,%maxn%) do set isf=!isf!!isu!
         echo(!sp!É!isf:~0,-1!»
         rem pause
         echo(!s!
      ) else (
      set s=!sp!!s!
      echo(!s!
   )
)
set isf=
if !lastn! gtr 0 (   for /L %%D in (1,1,%lastn%) do set isf=!isf!!is!Ê
   ) else       for /L %%D in (1,1,%maxn%) do set isf=!isf!!is!Ê
echo(!sp!È!isf:~0,-1!¼
rem choice /c:!keys! /n
ping ::1 -n 2 >nul
for %%N in (%errorlevel%) do echo(!code%%N!
rem pause

goto loop1



result:

Code: Select all

    ╔═════════════╗
    ║1:dog        ║
    ║2:cat        ║
    ║E:sheep      ║
    ║R:rabbit     ║
    ║T:chinchilla ║
    ║Y:monkey     ║
    ║U:human      ║
    ║I:alien      ║
    ║A:godzilla   ║
    ║S:orc        ║
    ║3:hamster    ║
    ║4:chicken    ║
    ║5:cow        ║
    ║6:horse      ║
    ║7:lizard     ║
    ║8:snake      ║
    ║Q:frog       ║
    ║W:llama      ║
    ╚═════════════╝
...
 ╔═════════════╦═════════════╦═════════════╦═════════════╗
 ║1:dog        ║2:cat        ║3:hamster    ║4:chicken    ║
 ║5:cow        ║6:horse      ║7:lizard     ║8:snake      ║
 ║Q:frog       ║W:llama      ║E:sheep      ║R:rabbit     ║
 ║T:chinchilla ║Y:monkey     ║U:human      ║I:alien      ║
 ║A:godzilla   ║S:orc        ╠═════════════╩═════════════╝
 ╚═════════════╩═════════════╝


Einstein1969

Re: How to create a option menu

Posted: 22 Oct 2013 22:35
by Batch Artist
einstein1969 wrote:modified version of dbenham:
- added contour
- auto center
- auto resize

demo with coiche commented, untested:

Code: Select all

:: dbenham StackOverflow Oct 18 2013
:: modified by einstein1969

@echo off
setlocal enableDelayedExpansion

rem set "cols=80"
set "cols=40"
set "lines=30"

:loop1

set /a "cols=cols+4"

mode con lines=%lines% cols=%cols%

set /a "cols-=3"

set "maxn="
set "n=0"
set "p=0"
set "keys="
for /f "delims==" %%A in ('set $prompt 2^>nul') do set "%%A="
for %%A in (
  "ÍÍÍÍÍÍÍÍÍÍÍÍÍ"
  "1:dog        "
  "2:cat        "
  "3:hamster    "
  "4:chicken    "
  "5:cow        "
  "6:horse      "
  "7:lizard     "
  "8:snake      "
  "Q:frog       "
  "W:llama      "
  "E:sheep      "
  "R:rabbit     "
  "T:chinchilla "
  "Y:monkey     "
  "U:human      "
  "I:alien      "
  "A:godzilla   "
  "S:orc        "
) do (
  set /a "n+=1, pn=p+1"
  if !n! equ 1 (set is=%%~A) else (
  for /f "tokens=1,2 delims= " %%P in ("!p! !pn!") do (
   set "tmp=!$prompt%%P!%%~Aº"
   if "!tmp:~%cols%!"=="" (set "$prompt%%P=!$prompt%%P!%%~Aº") else (
      if not defined maxn (set /a maxn=n-2, p+=1) else set /a "p+=1"
      set "$prompt%%Q=!$prompt%%Q!%%~Aº"
   )
  )
  for /f "tokens=1,2 delims=: " %%B in (%%A) do (
    set "keys=!keys!%%B"
    set "code!n!=%%C"
  )
  )
)

set /a "lastn=(n-1) %% maxn, i=lastn+1"
if !lastn! gtr 0 (
   set $prompt%p%=!$prompt%p%:~0,-1!Ì
   for /L %%D in (%i%,1,%maxn%) do set $prompt%p%=!$prompt%p%!!is!Ê
   set $prompt%p%=!$prompt%p%:~0,-1!¼
)

set "sp="
set "isf="
for /f "delims== tokens=1,2" %%A in ('set $prompt') do (
   set "s=º%%B"
   rem echo !s!
   if not defined sp (
      for /L %%C in (0,2,%cols%) do if "!s:~%cols%,1!"=="" (
               set "s= !s! "
               set "sp= !sp!"
            )
         if not defined sp (set "sp= "
               set "s= !s!"
         )
         rem set s=!s:~0,%cols%!
         rem echo '!s!' '!sp!'
         set isu=!is!Ë
         for /L %%D in (1,1,%maxn%) do set isf=!isf!!isu!
         echo(!sp!É!isf:~0,-1!»
         rem pause
         echo(!s!
      ) else (
      set s=!sp!!s!
      echo(!s!
   )
)
set isf=
if !lastn! gtr 0 (   for /L %%D in (1,1,%lastn%) do set isf=!isf!!is!Ê
   ) else       for /L %%D in (1,1,%maxn%) do set isf=!isf!!is!Ê
echo(!sp!È!isf:~0,-1!¼
rem choice /c:!keys! /n
ping ::1 -n 2 >nul
for %%N in (%errorlevel%) do echo(!code%%N!
rem pause

goto loop1



result:

Code: Select all

    ╔═════════════╗
    ║1:dog        ║
    ║2:cat        ║
    ║E:sheep      ║
    ║R:rabbit     ║
    ║T:chinchilla ║
    ║Y:monkey     ║
    ║U:human      ║
    ║I:alien      ║
    ║A:godzilla   ║
    ║S:orc        ║
    ║3:hamster    ║
    ║4:chicken    ║
    ║5:cow        ║
    ║6:horse      ║
    ║7:lizard     ║
    ║8:snake      ║
    ║Q:frog       ║
    ║W:llama      ║
    ╚═════════════╝
...
 ╔═════════════╦═════════════╦═════════════╦═════════════╗
 ║1:dog        ║2:cat        ║3:hamster    ║4:chicken    ║
 ║5:cow        ║6:horse      ║7:lizard     ║8:snake      ║
 ║Q:frog       ║W:llama      ║E:sheep      ║R:rabbit     ║
 ║T:chinchilla ║Y:monkey     ║U:human      ║I:alien      ║
 ║A:godzilla   ║S:orc        ╠═════════════╩═════════════╝
 ╚═════════════╩═════════════╝


Einstein1969


Woah that's pretty sweet! But it keep expanding, it doesn't stop expanding.
But that was pretty cool to see!

Re: How to create a option menu

Posted: 22 Oct 2013 22:45
by carlsomo
Here is a header format I like to use with menus

Code: Select all

:HEADER
MODE CON: COLS=80 LINES=50
COLOR 1E
SET DISPLYDT=10/22/2012
SET "DAYOFWK=TUESDAY   "
SET "DAYOFWK=%DAYOFWK:~0,9%"
ECHO.                      ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
ECHO ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͵     Menu Selection Utility    ÆÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
ECHO º                     ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ                       º
ECHO º                                                                             º
ECHO º              Menu Information Goes here center it yourself                  º
ECHO º                                                                             º
ECHO º         Description of what happens or why we're here goes here             º
ECHO º                                                                             º
ECHO º               (c) 2013 Carl Smith Information Technology                    º
ECHO º                                                                             º
ECHO º  %DAYOFWK%                                                      %DISPLYDT%  º
ECHO ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ECHO.
ECHO.
PAUSE
EXIT /B 0

Re: How to create a option menu

Posted: 23 Oct 2013 03:40
by Batch Artist
carlsomo wrote:Here is a header format I like to use with menus

Code: Select all

:HEADER
MODE CON: COLS=80 LINES=50
COLOR 1E
SET DISPLYDT=10/22/2012
SET "DAYOFWK=TUESDAY   "
SET "DAYOFWK=%DAYOFWK:~0,9%"
ECHO.                      ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
ECHO ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͵     Menu Selection Utility    ÆÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
ECHO º                     ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ                       º
ECHO º                                                                             º
ECHO º              Menu Information Goes here center it yourself                  º
ECHO º                                                                             º
ECHO º         Description of what happens or why we're here goes here             º
ECHO º                                                                             º
ECHO º               (c) 2013 Carl Smith Information Technology                    º
ECHO º                                                                             º
ECHO º  %DAYOFWK%                                                      %DISPLYDT%  º
ECHO ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
ECHO.
ECHO.
PAUSE
EXIT /B 0


Hmm that is interesting and cool. Looks good and works like a charm! Marvelous!