1

I'm trying to create a batch file which asks a user a question with multiple (four) options. The options that the user chooses should trigger the batch file to ignore one of the four options.

Lets say that the commands that I would normally want to run is ECHO "A", ECHO "B", ECHO "C", ECHO "D". If the person puts A in, I would want them to still run all the other commands except ECHO "A".

Currently the solution i have come up with is the following

@echo off
set /p choice="Do you want not to do: A, B, C or D? [A/B/C/D] " [A/B/C/D]
if %choice%==A (
  ECHO "B"
  ECHO "C"
  ECHO "D"
  EXIT /b
) else if %choice%==B (
  ECHO "A"
  ECHO "C"
  ECHO "D"
  EXIT /b
) else if %choice%==C (
  ECHO "A"
  ECHO "B"
  ECHO "D"
  EXIT /b
) else if %choice%==D ( 
  ECHO "A"
  ECHO "B"
  ECHO "C"
  EXIT /b
) ELSE ( 
  ECHO Please select A, B, C or D!
  EXIT /b
)

However as you can imagine, i am using the echo commands as an example/abstraction. The actual commands are longer. It seems more efficient to define the commands under A, B, C and D once and then tell the batch file not to doA, B, C or D depending the users choice.

Maybe it's because I started working early today but I cannot think of an easier way to do this right now. So I hope to hear your ideas on how to make this batch script less clogged up.

LarsTech
  • 80,625
  • 14
  • 153
  • 225

4 Answers4

2

The idea in this code is that it only skips the one that you choose.

@echo off
set /p choice="Do you want not to do: A, B, C or D? [A/B/C/D] " [A/B/C/D]
if "%choice%"=="" (
    ECHO Please select A, B, C or D!
    EXIT /b
)
if not %choice%==A (
   ECHO "A"
) 
if not %choice%==B (
   ECHO "B"
) 
if not %choice%==C (
   ECHO "C"
) 
if not %choice%==D (
   ECHO "D"
) 
exit /b
user1261104
  • 315
  • 4
  • 14
  • I was thinking of something along those lines, but wasn't sure on how to use the 'if not' statement. Thanks for the answer! –  Nov 06 '14 at 21:03
1

Something like this :

@echo off
setlocal enabledelayedexpansion
set "$Command=A B C D"
set /p "$choice=Do you want not to do: A, B, C or D? "
set "$Command=!$Command:%$choice%=!"
for %%a in (!$command!) do echo %%a
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • A little bit harder to understand than my code, but very nice and compact! One question though, why the use of $ signs? I already find batch hard enough to read with code like `set AdrBase=%AdrBase:"=%` (replace the " symbol with nothing), why would you make it even harder by adding dollar signs ;) – user1261104 Nov 06 '14 at 16:24
  • That's just my way of setting variables. – SachaDee Nov 06 '14 at 17:12
  • Nice and compact, exactly what I was looking for. I will try to implement it properly tomorrow in the actual script I am working on. –  Nov 06 '14 at 21:04
0

This should do the trick.

After a "CALL" the batch "jumps back" and will do the next step:

@echo off
set /p choice="Do you want not to do: A, B, C or D? [A/B/C/D] " [A/B/C/D]
if %choice%==A (
  CALL :B
  CALL :C
  CALL :D
  EXIT /b
) else if %choice%==B (
  CALL :A
  CALL :C
  CALL :D
  EXIT /b
) else if %choice%==C (
  CALL :A
  CALL :B
  CALL :D
  EXIT /b
) else if %choice%==D ( 
  CALL :A
  CALL :B
  CALL :C
  EXIT /b
) ELSE ( 
  ECHO Please select A, B, C or D!
  EXIT /b
)

:A
<Function here>
GOTO :EOF

:B
<Function here>
GOTO :EOF

:C
<Function here>
GOTO :EOF

:D
<Function here>
GOTO :EOF
BaBa
  • 337
  • 2
  • 10
0

This type of problems may be easily solved using an array; the code is clearer and more flexible. I also suggest you to use choice command instead of set /P in order to achieve this type of selections.

@echo off
setlocal EnableDelayedExpansion

rem Define all the options/commands
set option[1]=ECHO "A"
set option[2]=ECHO "B"
set option[3]=ECHO "C"
set option[4]=ECHO "D"

choice /C ABCD /M "Do you want not to do: A, B, C or D"

rem Eliminate the not wanted option
set "option[%errorlevel%]="

rem Execute the remaining options
for /F "tokens=1* delims==" %%a in ('set option[') do %%b
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108