Jump to content

[Batch] How to do a Multi Choice Selector script?


Recommended Posts

Hi, :)
I'm trying to do a Multi Choice Selector script where a user can presses 1,2,3 to select the options he wants to run after he pressing ENTER.
When a user selected an option it should turn from  [NO] to [YES] (Green) and if the user selected it again it should turn back to [NO] .

1. [YES] Option1
2. [NO] Option2
3. [YES] Option3

I know how to do a script where a user can enter the options separated with a conman, but this script above i try to do is way more complicated and i don't know where to even start with it. :}

Edited by Mike86
Link to comment
Share on other sites


The probem is not with the script about the selection, but rather with showing the selection,

You need to re-draw the whole screen, ( and you need some ANSI code) for the colours.

ANSI codes example:

https://gist.github.com/mlocati/fdabcaeb8071d5c75a2d51712db24011

Generic redraw/set options:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

SET Option1=NO
SET Option2=NO
SET Option3=NO

CALL :drawloop
SET /P Option1= Option1? [%Option1%]
SET /P Option2= Option2? [%Option2%]
SET /P Option3= Option3? [%Option3%]
CALL :drawloop
PAUSE
GOTO :EOF

:drawloop
CLS
ECHO 1. [%option1%] Option1
ECHO 2. [%option2%] Option2
ECHO 3. [%option3%] Option3
GOTO :EOF

jaclaz

Link to comment
Share on other sites

I'm a little bit confused now. I don't see how to do what i had in mind with this script.
Here is an example of the next best thing i could do without the option toggle function (Which would be more user friendlier).

@echo off

echo.
echo Selection time!
echo.
echo 1. My father is Joe
echo 2. My mother is Audrey
echo 3. My brother is Jerry
echo.

:getOptions
set /p "choices=Type in the option numbers separated by a comma (Example 1,3,4): "


if not defined choices ( 
    echo Please enter a valid option
    goto getOptions
    )

for %%a in (%choices%) do if %%a EQU 6 set choices=1,2,3,4,5
for %%i in (%choices%) do call :extract & :option-%%i

echo.
echo Done
pause
exit

:option-1
echo My father is Joe >
pause
exit /B

:option-2
echo My mother is Audrey
pause
exit /B

:option-3
echo My brother is Jerry
pause
exit /B

 

Edited by Mike86
Link to comment
Share on other sites

Exactly, use redraw menu. Choice is a too a good option:

choice_menu.gif.944019d27effa970bec3f93feeddf2fb.gif

@Echo Off

Set on=[YES]
Set off=[NO]

Set opt1=%off%
Set opt2=%off%
Set opt3=%off%

:menu
Cls
Echo 1. %opt1% Option_1
Echo 2. %opt2% Option_2
Echo 3. %opt3% Option_3
Echo 4. GO

Choice /C 1234 /N /M "Toggle your option: "

If ERRORLEVEL 4 GoTo :continue
If ERRORLEVEL 3 (If %opt3%==%on% (Set opt3=%off%) Else (Set opt3=%on%)) & GoTo :menu
If ERRORLEVEL 2 (If %opt2%==%on% (Set opt2=%off%) Else (Set opt2=%on%)) & GoTo :menu
If ERRORLEVEL 1 (If %opt1%==%on% (Set opt1=%off%) Else (Set opt1=%on%)) & GoTo :menu

:continue
Pause

 

Link to comment
Share on other sites

2 hours ago, EdSon said:

Exactly, use redraw menu. Choice is a too a good option:

choice_menu.gif.944019d27effa970bec3f93feeddf2fb.gif


@Echo Off

Set on=[YES]
Set off=[NO]

Set opt1=%off%
Set opt2=%off%
Set opt3=%off%

:menu
Cls
Echo 1. %opt1% Option_1
Echo 2. %opt2% Option_2
Echo 3. %opt3% Option_3
Echo 4. GO

Choice /C 1234 /N /M "Toggle your option: "

If ERRORLEVEL 4 GoTo :continue
If ERRORLEVEL 3 (If %opt3%==%on% (Set opt3=%off%) Else (Set opt3=%on%)) & GoTo :menu
If ERRORLEVEL 2 (If %opt2%==%on% (Set opt2=%off%) Else (Set opt2=%on%)) & GoTo :menu
If ERRORLEVEL 1 (If %opt1%==%on% (Set opt1=%off%) Else (Set opt1=%on%)) & GoTo :menu

:continue
Pause

 

Thanks do  you also know how to turn the YES to green?

Edited by Mike86
Link to comment
Share on other sites

Here a demo menu cmd I made to turn on or 

@Echo Off
CLS
COLOR 9F
MODE 62,9
TITLE Demo Menu

:Main
CLS
Echo.
Echo  This Is To Turn On Or Turn Off The Hiberfil.sys
Echo.
Echo   Type YES For This Choice
Echo   Type NO For This Choice
Echo   Type Quit To Close Window
Echo.

SET Choice=
SET /P Choice=Type Choice Then Press Enter: 


IF /I '%Choice%'=='yes' GOTO Item1
IF /I '%Choice%'=='no' GOTO Item2
IF /I '%Choice%'=='quit' GOTO TheEnd


ECHO "%Choice%" is not valid. Please try again.
SET /P = Press Enter To Continue
GOTO MAIN


:Item1
CLS
Echo.
Echo You Selected Choice 1
ping -n 3 127.0.0.1>nul
Goto TheEnd


:Item2
CLS
Echo.
Echo You Selected Choice 2
ping -n 3 127.0.0.1>nul
Goto TheEnd

:TheEnd
Exit

 

Link to comment
Share on other sites

This below is more what i was trying to do. :) The only thing i miss is turning YES into Green (If possible on Win2k/XP too) and to use alphabetic key inputs instead of only numbers.:sneaky:

@Echo Off

Set on=[YES]
Set off=[NO]
Set opt1=%off%
Set opt2=%off%
Set opt3=%off%

:menu
Cls
Echo 1. %opt1% Option 1.
Echo 2. %opt2% Option 2.
Echo 3. %opt3% Option 4.
Echo 4. GO

Choice /C 1234 /N /M "Toggle your option: "

If ERRORLEVEL 4 GoTo :continue
If ERRORLEVEL 3 (If %opt3%==%on% (Set opt3=%off%) Else (Set opt3=%on%)) & GoTo :menu
If ERRORLEVEL 2 (If %opt2%==%on% (Set opt2=%off%) Else (Set opt2=%on%)) & GoTo :menu
If ERRORLEVEL 1 (If %opt1%==%on% (Set opt1=%off%) Else (Set opt1=%on%)) & GoTo :menu

:continue

ECHO Always execute this code when an option is selected.
pause

If %opt1%==%off% GOTO SKIP
ECHO Execute Option 1 code...
:SKIP

If %opt2%==%off% GOTO SKIP
ECHO Execute Option 2 code...
:SKIP

If %opt3%==%off% GOTO SKIP
ECHO Execute Option 3 code...
:SKIP

pause
EXIT

 

Edited by Mike86
Link to comment
Share on other sites

Nop, the color feature is posible on all Windows (incl. XP) using FindStr:

choice_color_menu.gif.92ce18ddf51aef163ed5ffda073dba63.gif

@Echo Off

Set "on=[YES]"
Set "off=[NO] "

Set opt1=%off%
Set opt2=%off%
Set opt3=%off%

:menu
Cls
<NUL Set/P=A. & ( If %opt1%==%on% (Call :echolor %opt1% 0a) Else (<NUL Set/P=%opt1%) ) & Echo  Option_1
<NUL Set/P=B. & ( If %opt2%==%on% (Call :echolor %opt2% 0a) Else (<NUL Set/P=%opt2%) ) & Echo  Option_2
<NUL Set/P=C. & ( If %opt3%==%on% (Call :echolor %opt3% 0a) Else (<NUL Set/P=%opt3%) ) & Echo  Option_3
Echo D. GO

Choice.exe /C ABCD /N /M "Toggle your option: "

If ERRORLEVEL 4 GoTo :continue
If ERRORLEVEL 3 (If %opt3%==%on% (Set opt3=%off%) Else (Set opt3=%on%)) & GoTo :menu
If ERRORLEVEL 2 (If %opt2%==%on% (Set opt2=%off%) Else (Set opt2=%on%)) & GoTo :menu
If ERRORLEVEL 1 (If %opt1%==%on% (Set opt1=%off%) Else (Set opt1=%on%)) & GoTo :menu

:continue
Pause
Goto :EOF

:echolor (text, color)
  MD "%tmp%\_%1" >NUL
  Pushd "%tmp%\_%1"
  For /F %%a In ('Echo PROMPT $H ^| Cmd.exe') do Set "bs=%%a"
  <nul Set /P="_" >"%1"
  FindStr.exe /S /B /P /A:%2 /C:"_" "%1"
  <nul Set /P=%bs%%bs%
  Popd
  RD /S /Q "%tmp%\_%1"
Goto :EOF

- Make a temp directory for reduce FindStr recursive finder to minimum

- Pushd and Popd for save, change and recovery the current directory

- For Echo PROMPT $H, for obtain BackSpace character

- FindStr colorize the folder containing the searched string followed by a colon and the string

- Use BackSpace variable for delete colon and string

- Use <NUL Set/P="hello" for Echo without newline endening

 

- Windows XP not include Choise.exe command T_T. BUT you can use Set /P asking to press Enter Key modifying the menu like this: 

:menu
  Cls
  <NUL Set/P=A. & ( If %opt1%==%on% (Call :echolor %opt1% 0a) Else (<NUL Set/P=%opt1%) ) & Echo  Option_1
  <NUL Set/P=B. & ( If %opt2%==%on% (Call :echolor %opt2% 0a) Else (<NUL Set/P=%opt2%) ) & Echo  Option_2
  <NUL Set/P=C. & ( If %opt3%==%on% (Call :echolor %opt3% 0a) Else (<NUL Set/P=%opt3%) ) & Echo  Option_3
  Echo D. GO

  Set /P op="Toggle your option AND PRESS ENTER: "

  If /I "%op%"=="D" GoTo :continue
  If /I "%op%"=="C" (If %opt3%==%on% (Set opt3=%off%) Else (Set opt3=%on%)) & GoTo :menu
  If /I "%op%"=="B" (If %opt2%==%on% (Set opt2=%off%) Else (Set opt2=%on%)) & GoTo :menu
  If /I "%op%"=="A" (If %opt1%==%on% (Set opt1=%off%) Else (Set opt1=%on%)) & GoTo :menu
GoTo :menu

Result:

choice_color_menu_XP.gif.1675540f66b33dc34a6bffcc69a8693f.gif

 

- Another option for colorize is using Call to PowerShell: https://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line

Edited by EdSon
Update Windows XP support
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...