Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple choices menu on batch file?

Tags:

batch-file

Hi I want to make a batch file menu, that asks 'Select app you want to install?' for example

  1. App1
  2. App2
  3. App3
  4. App4
  5. App5
  6. ALL Apps

Select what app:_

What I want is, for example I want to install App2, App3, and App5, so I can type on by App ID's 'Select what app:2,3,5' . And when user Select option 6, it will install all Applications!

I know this is possible on bash scripting, but Im not sure on batch scripting?

An example of batch menu is http://mintywhite.com/software-reviews/productivity-software/create-multiple-choice-menu-batchfile/

like image 890
Insomia Pierre Avatar asked Jan 25 '13 19:01

Insomia Pierre


3 Answers

Answer

This will do what you want. Let me know if you have any questions. All you have to do is follow the two steps listed in the script.

Script

:: Hide Command and Set Scope
@echo off
setlocal EnableExtensions

:: Customize Window
title My Menu

:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps
:: Step 1. List the Application Names
set "App[1]=One"
set "App[2]=Two"
set "App[3]=Three"
set "App[4]=Four"
set "App[5]=Five"
set "App[6]=All Apps"

:: Display the Menu
set "Message="
:Menu
cls
echo.%Message%
echo.
echo.  Menu Title
echo.
set "x=0"
:MenuLoop
set /a "x+=1"
if defined App[%x%] (
    call echo   %x%. %%App[%x%]%%
    goto MenuLoop
)
echo.

:: Prompt User for Choice
:Prompt
set "Input="
set /p "Input=Select what app:"

:: Validate Input [Remove Special Characters]
if not defined Input goto Prompt
set "Input=%Input:"=%"
set "Input=%Input:^=%"
set "Input=%Input:<=%"
set "Input=%Input:>=%"
set "Input=%Input:&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
call :Validate %Input%

:: Process Input
call :Process %Input%
goto End


:Validate
set "Next=%2"
if not defined App[%1] (
    set "Message=Invalid Input: %1"
    goto Menu
)
if defined Next shift & goto Validate
goto :eof


:Process
set "Next=%2"
call set "App=%%App[%1]%%"

:: Run Installations
:: Specify all of the installations for each app.
:: Step 2. Match on the application names and perform the installation for each
if "%App%" EQU "One" echo Run Install for App One here
if "%App%" EQU "Two" echo Run Install for App Two here
if "%App%" EQU "Three" echo Run Install for App Three here
if "%App%" EQU "Four" echo Run Install for App Four here
if "%App%" EQU "Five" echo Run Install for App Five here
if "%App%" EQU "All Apps" (
    echo Run Install for All Apps here
)

:: Prevent the command from being processed twice if listed twice.
set "App[%1]="
if defined Next shift & goto Process
goto :eof


:End
endlocal
pause >nul
like image 129
David Ruhmann Avatar answered Oct 16 '22 20:10

David Ruhmann


you may use choice.exe see here : http://ss64.com/nt/choice.html

like image 5
Loïc MICHEL Avatar answered Oct 16 '22 20:10

Loïc MICHEL


You want to use set /p Example below:

echo What would you like to install?
echo 1 - App1
echo 2 - App2

set /p whatapp=

if %whatapp%==1 (
    codetoinstallapp1
) else if %whatapp%==2 (
    codetoinstallapp2
) else (
    echo invalid choice
)
like image 5
Patrick Meinecke Avatar answered Oct 16 '22 21:10

Patrick Meinecke