scripting problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 14 Aug 2020 09:58

scripting problem

#1 Post by Barc6117 » 14 Aug 2020 10:21

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
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
if %errorlevel%== w goto :menu3
if %errorlevel%== s goto :menu2
if %errorlevel%== x goto :Newgame

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

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
Last edited by Squashman on 14 Aug 2020 14:01, edited 1 time in total.
Reason: MOD EDIT: Please use code tags.

Posts: 4197
Joined: 23 Dec 2011 13:59

Re: scripting problem

#2 Post by Squashman » 14 Aug 2020 14:06

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

   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.

Posts: 80
Joined: 06 May 2020 10:14

Re: scripting problem

#3 Post by T3RRY » 16 Aug 2020 03:39

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.

Post Reply