English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to make a easy batch file and have used the "choice" command to display 1,2,3,4,5 for the options. How do I proceed so that the corresponding keypress will execute the appropriate external following batch file that corresponds to the number pressed? I have tried to use the "IF" command and was only able to get "1" to work. All following keypresses executed the same batch as pressing "1". Kinda lost and new to the wonderful world of dos. Thanks for any help.

2007-08-15 12:23:17 · 0 answers · asked by C Y 1 in Computers & Internet Programming & Design

0 answers

For your testing purposes, I would make a slightly easier batch file. The following code snippet assumes that you want to use the numerals 1-5 as your possible choices and also that you have named your batch files 1.bat, 2.bat, 3.bat, 4.bat, and 5.bat for ease of use.

================
@echo off
cls
choice /c:12345 Pick a number:
call %errorlevel%.bat

================

If you want to get a little fancier and add a menu, then use the following, and modify the text of the menu however you want,

@echo off
cls
echo 1. ONE batch
echo 2. TWO batch
echo 3. THREE batch
echo 4. FOUR batch
echo 5. FIVE batch
choice /c:12345 Pick a number:
call %errorlevel%.bat

=================

You could get even more complicated and name your batch files something entirely different than your available keypress choices, but why? But, if you really did, then you could do the following .. assuming you have batch files called one.bat, two.bat, three.bat, four.bat, and five.bat for this test.

@echo off
cls
echo 1. ONE batch
echo 2. TWO batch
echo 3. THREE batch
echo 4. FOUR batch
echo 5. FIVE batch
choice /c:12345 Pick a number:

if "%errorlevel%"=="1" call ONE.bat
if "%errorlevel%"=="2" call TWO.bat
if "%errorlevel%"=="3" call THREE.bat
if "%errorlevel%"=="4" call FOUR.bat
if "%errorlevel%"=="5" call FIVE.bat


=================

And, finally, if you wanted to keep your individual batch functions all wrapped in the same master batch file, then just make separate sections for each function. I like this one the best. One file for all your code = easy to keep track of. But, separate sections for each function that you're calling = easy to edit and organize.

@echo off
cls
echo 1. ONE batch
echo 2. TWO batch
echo 3. THREE batch
echo 4. FOUR batch
echo 5. FIVE batch
choice /c:12345 Pick a number:
call :%errorlevel%
goto :EOF

:1
echo ONE
goto :EOF

:2
echo TWO
goto :EOF

:3
echo THREE
goto :EOF

:4
echo FOUR
goto :EOF

:5
echo FIVE
goto :EOF

2007-08-16 15:41:27 · answer #1 · answered by Kevin 7 · 5 0

Batch File Choice

2017-01-03 12:47:44 · answer #2 · answered by ? 4 · 0 0

This Site Might Help You.

RE:
Created batch file menu to choose 5 choices. Got choice command to display 1,2,3,4,5. How to proceed now?
I am trying to make a easy batch file and have used the "choice" command to display 1,2,3,4,5 for the options. How do I proceed so that the corresponding keypress will execute the appropriate external following batch file that corresponds to the number pressed? I have tried to use the...

2015-08-13 01:31:16 · answer #3 · answered by Anonymous · 0 0

Try this sample batch file. Substitute the CALL statement with your own batch file, for instance, if you have a batch file called "SHORTY.BAT" use CALL SHORTY. Get it? Hope this helps.
------------------------------
The following batch file snippet displays a simple menu (without a question-mark at the end of the prompt) and prompts for the users choice, defaulting to option 2 after 5 seconds. Copy all text BELOW THIS LINE into a batch file:

ECHO 1. MS-DOS Editor.
ECHO 2. MS-Windows. (default)
ECHO 3. Defrag the hard-drive.
ECHO 4. Quit.
CHOICE /C:1234 /N /T:2,5 Please choose a menu option.
IF ERRORLEVEL == 4 GOTO QUIT_MENU
IF ERRORLEVEL == 3 GOTO DEFRAG_HD
IF ERRORLEVEL == 2 GOTO RUN_WIN
IF ERRORLEVEL == 1 GOTO RUN_EDIT
:RUN_EDIT
CALL EDIT
:RUN_WIN
CALL WIN
:DEFRAG_HD
DEFRAG c:
:QUIT_MENU
ECHO Safe to switch off machine now...

2007-08-15 12:42:53 · answer #4 · answered by GhettoCyrano 2 · 0 1

fedest.com, questions and answers