Casey French
Casey French

Reputation: 139

Is it possible to create a sub-menu in a windows batch file?

I am currently writing a batch file that will output a plain text file in the format of a scripting language that an application on my phone uses to automate screen touches. What I would like to do is create a sub-menu that is called when only specific options in my main menu are chosen. So here is the current version on my batch file. The problem I am having is when I try to create a sub-menu my action for Yes is not executed, it just returns to the main menu.

Here is what I have so far.

:MENU
ECHO.
ECHO ###################################################
ECHO # Select an Option to begin building your script  #
ECHO ###################################################
ECHO.
ECHO 1 - START
ECHO 2 - SEARCH
ECHO 3 - X_COORDINATES
ECHO 4 - Y_COORDINATES
ECHO 5 - 1
ECHO 6 - 2
ECHO 7 - 3
ECHO 8 - 4
ECHO 9 - 5
ECHO 10 - 6
ECHO 11 - 7
ECHO 12 - 8
ECHO 13 - 9
ECHO 14 - 0
ECHO 15 - QUIT
ECHO.
SET /P M=TYPE 1-15 THEN PRESS ENTER:
IF %M%==1 GOTO START
IF %M%==2 GOTO SEARCH
IF %M%==3 GOTO X_COORDINATES
IF %M%==4 GOTO Y_COORDINATES
IF %M%==5 GOTO 1
IF %M%==6 GOTO 2
IF %M%==7 GOTO 3
IF %M%==8 GOTO 4
IF %M%==9 GOTO 5
IF %M%==10 GOTO 6
IF %M%==11 GOTO 7
IF %M%==12 GOTO 8
IF %M%==13 GOTO 9
IF %M%==14 GOTO 0
IF %M%==15 GOTO QUIT

This code block below is what I am having trouble with. When "Y" is selected, it does not print the line of touchPress 0 1315 2451 or the sleep 590 line of script code into my file that I will later use in the application to automate searching a game map from end to end without having to manually type whatever the combinations are on a 1200x1200 grid. It simply returns to the menu like "N" was chosen.

(I chose to use @ECHO OFF for the sub-menu because I didn't want to confuse myself or anyone else that may use this to write their own searching script by printing it in the console window before they ever even started using the batch file. I only wanted the sub-menu to be shown when certain options were chosen from the main menu.)

@ECHO OFF
:SUB-MENU
SET /P M=TYPE Y,N THEN PRESS ENTER:
IF %M%==Y @ECHO touchPress 0 1315 2451>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
@ECHO sleep 590>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
IF %m%==N GOTO MENU

This is just a break to place emphasis on the problem block in the batch file.

ECHO.
:START
cd..
cd..
cd C:\Documents and Settings\
cd C:\Documents and Settings\Master Terminal\
cd C:\Documents and Settings\Master Terminal\Desktop\
cd C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles
@echo :start>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
GOTO :MENU
:SEARCH
@ECHO touchPress 0 493 2148>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
@ECHO sleep 590>>speedSearch.txt
GOTO :MENU
:X_COORDINATES
@ECHO touchPress 0 529 1221>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
@ECHO sleep 590>>speedSearch.txt
GOTO :MENU
:Y_COORDINATES
@ECHO touchPress 0 1131 1208>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
@echo sleep 590>>speedSearch.txt
GOTO :MENU
:1
@ECHO touchPress 0 220 1800>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
@ECHO sleep 590>>speedSearch.txt
GOTO :SUB-MENU

Any advice is much appreciated. I have an additional question to this project, but I would like to get this portion solved before digging further into problems.

Upvotes: 1

Views: 1399

Answers (2)

Aacini
Aacini

Reputation: 67216

Although your post is extensive, the question is pretty confusing; the short answer to the question as stated is: "Yes". You had not posted a single example of "the sub-menu problem"; it seems that your problem is related to "Why an IF comparison with a variable fails?". You had posted a load of code unrelated to the problem, so I posted a large answer also unrelated to the problem. ;-)

The simplest way to get menu options from the user is via choice command; this is both simpler to the user (that just have to press one key) and to the program because you know that the answer of choice is always valid, so no further testing is needed.

The simplest way to repeat the same code several times with different values is using the array concept; this way, the code is written just one time and the appropriate value for each case is selected from an array via an index.

The program below is equivalent to your code, but include the previously described concepts. Perhaps the "sub-menu" part is not complete, but you had not described in which cases your code use the "sub-menu", so I assumed that is when the user enter a digit.

@echo off
setlocal EnableDelayedExpansion

rem Define the array of coordinates per menu options: QBSXY1234567890
set i=2
for %%a in ("493 2148" "529 1221" "1131 1208" "220 1800" "etc...") do (
   set /A i+=1
   set "coords[!i!]=%%~a"
)

:MENU
ECHO/
ECHO ###################################################
ECHO # Select an Option to begin building your script  #
ECHO ###################################################
ECHO/
ECHO B - BEGIN
ECHO S - SEARCH
ECHO X - X_COORDINATES
ECHO Y - Y_COORDINATES
ECHO Digit between 0 and 9
ECHO Q - QUIT
ECHO/
:OPTION-0
CHOICE /C QBSXY1234567890 /N /M "SELECT OPTION: "
SET "OPTION=%ERRORLEVEL%"

rem Process options Quit and Begin
IF %OPTION% LEQ 2 GOTO OPTION-%OPTION%

rem Otherwise the option is Search, X_coords, Y_coords or a Digit:
rem get the value of the corresponding array element
ECHO touchPress 0 !coords[%OPTION%]!>> speedSearch.txt
ECHO sleep 590>> speedSearch.txt

rem If the option was Search, X_coords or Y_coords: it is complete
IF %OPTION% LEQ 5 GOTO MENU

rem Otherwise the option is a digit: enter the infamous "sub-menu"
:SUB-MENU
CHOICE /C NY /M "TYPE Y,N"
IF ERRORLEVEL 2 (
   ECHO touchPress 0 1315 2451>> speedSearch.txt
   ECHO sleep 590>> speedSearch.txt
)
GOTO :MENU

:OPTION-2  START
cd "C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles"
echo :start> speedSearch.txt
GOTO :MENU

:OPTION-1  QUIT
GOTO :EOF

Upvotes: 2

Mofi
Mofi

Reputation: 49097

First, you can use instead of

cd..
cd..
cd C:\Documents and Settings\
cd C:\Documents and Settings\Master Terminal\
cd C:\Documents and Settings\Master Terminal\Desktop\
cd C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles

simple the single line

cd /D "C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles"

or perhaps even better

cd /D "%USERPROFILE%\Desktop\speedSearchFiles"

if the user account name is Master Terminal.

Second, even if you describe what a batch user should enter on prompt, the batch user can always simply press key RETURN without entering anything. So better predefine the variable with a default value for this case.

And the batch user can also enter any string and not just what you recommend. So prepare the batch file for any user input as well.

@ECHO OFF
:SUB-MENU
SET "M=N"
SET /P "M=TYPE Y,N THEN PRESS ENTER: "
REM Prevent a syntax error if user enters 1 or more double quotes by removing them.
SET "M=%M:"=%"
IF /I "%M%"=="Y" ECHO touchPress 0 1315 2451>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
ECHO sleep 590>>"C:\Documents and Settings\Master Terminal\Desktop\speedSearchFiles\speedSearch.txt"
GOTO MENU

y or Y is interpreted as YES, everything else is interpreted as NO.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • echo /?
  • goto /?
  • if /?
  • rem /?
  • set /?

Upvotes: 2

Related Questions