Page 1 of 1

scripting problem

Posted: 14 Aug 2020 10:21
by Barc6117
Alright SO I want to make a menu for a batch File but it won't work
here's the code.

Code: Select all

@echo off
set /a %bee%=1
:menu
if %bee%==1 echo WS to move up or down, Also X to say yes
choice /c WSX /n
echo #play
echo load
echo exit
pause
cls
if %errorlevel%== w goto :menu3
if %errorlevel%== s goto :menu2
if %errorlevel%== x goto :Newgame

:menu2
if %bee%==1 set /a bee = 0
choice /c WSX /n
echo play
echo #load
echo exit
pause
if %errorlevel%== w goto :menu
if %errorlevel%== s goto :menu3
if %errorlevel%== x goto :load

:menu3
if %bee%==1 set /a bee = 0
choice /c WSX /n
if %errorlevel%== w goto :menu2
if %errorlevel%== s goto :menu
if %errorlevel%== x goto :exit

echo play
echo load
echo #exit
pause

Re: scripting problem

Posted: 14 Aug 2020 14:06
by Squashman
Couple of problems with your code. Reading the help file for the commands you are using would help you in both cases. You can read the help for a command by opening a command prompt and typing the command name followed by a /?.

Code: Select all

H:\>set /?
The percent symbols are not used when assigning a value to a variable. They are only used when accessing the value of a variable. So remove the percent symbols.

Code: Select all

set /a bee=1
Also read this note in the help for the CHOICE command.

Code: Select all

   NOTE:
   The ERRORLEVEL environment variable is set to the index of the
   key that was selected from the set of choices. The first choice
   listed returns a value of 1, the second a value of 2, and so on.
   If the user presses a key that is not a valid choice, the tool
   sounds a warning beep. If tool detects an error condition,
   it returns an ERRORLEVEL value of 255. If the user presses
   CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
   of 0. When you use ERRORLEVEL parameters in a batch program, list
   them in decreasing order.
The value of the choice is not the same as the choice they selected.

Re: scripting problem

Posted: 16 Aug 2020 03:39
by T3RRY
Not also there are simpler ways of constructing a batch file using choice to navigate menu's than cumbersome if conditions.

A simple example:

Code: Select all

Echo/ [1] Option 1 [2] Option 2 [3] Option 3
For /F "Delims=" %%O in ('Choice /N /C:123') Do goto :1stmenu%%O
Using a for /f loop to iterate over the choice command returns the literal key pressed, so the keys you flag as choice options are up to you, they just need to match the label names you target with goto.

The same trick can be used with delayed expansion to expand simple macro commands that vary depending on the selection made. Depending on the commands to be issued ind the other labels, it may be more practical to assign the / those commands to variables in array style and use the macro approach.