Something wrong with my batch script? - batch-file

I'm having to do a project on figurative language in Maroon 5 music, and decided to make a quiz. I am trying to be able to count how many questions were right. Here is my work so far, and there is a bug somewhere, but I don't know what it is.
#echo off
color 0f
:START
set Q=Q
cls
echo Maroon 5 Figuratve Language
echo ---------------------------
echo Options (Type a number and press ENTER):
echo 1) Take quiz
echo 2) Change color scheme
set /p Choice=
if %Choice% == 1 GOTO GAME
if %Choice% == 2 GOTO COLOR
:COLOR
cls
echo Choose a color scheme.
echo 1) Black BG, White text
echo 2) White BG, black text
set /p color=
if %color% == 1 GOTO 0F
if %color% == 2 GOTO F0
:0F
color 0f
GOTO start
:F0
color f0
GOTO start
:GAME
set qr=0
cls
goto Q1
:Q1
set qn=1
cls
echo What type of figurative language is "I can smell your scent from miles?"
echo a) Simile
echo b) Hyperbole
echo c) Metaphor
echo d) Personification
set /p Q1=
if %Q1% == b goto CORRECT
if NOT %Q1% == b goto INCORRECT
:CORRECT
set /a qr=%qr%+1
set /a qn=%qn%+1
goto %Q%%qn%
:INCORRECT
cls
echo Incorrect...
pause
set /a qn=%qn%+1
goto %Q%%qn%
:Q2
echo testing
echo %qn%
echo %qr%
pause

I can see a bug (there might be more in the code but this one is the first I can see ^^)! :D It's in the line goto %Q%%qn%. I've answered a similar question time ago: How to put variable value inside another variable name in batch?
The point is that as your variables are being evaluated at parse time the interpreter doesn't know what's the value of %Q% and %qn%. You have to add SETLOCAL EnableDelayedExpansion at the beginning of your script and surround your variables with !...! instead of %...%. This will force the interpreter to evaluate the values at run time considering any changes made to the variables so far.

Related

Making a batch DND assistant

I don't know if any of you guys know anything about batch (I am sure someone does), but in regards to this post, I am trying to create a dumbed down DND Assist (something that you would tell you stats to and that would assist you in completing an action irl faster than rolling dice and doing the math yourself.
Currently I have the random num generator somewhat working (although I would like to improve it)
This version is set to choose a random number between 1 and 20, I would like to figure out a way to have the program notice if you roll a 1 or a 20 (Crit hits / crit fail)
Also I need something later that will show me how to save certain values as variables,
#echo off
:Start
Set /a ans="%RANDOM% %% 20"+1
echo %ans%
pause
goto Start
(In regards to saving variables, when the program is ran, it will tell me that I am missing an operation.)
The coloring portion was just for the hecks of it, if someone can show me a way to streamline that section please tell.
Please use lamens terms, Im still not very good at understanding any of this.
(CURRENT ASSIST PROGRAM PROGRESS)
#echo off
cls
:BEGIN
Echo HI THERE! AND WELCOME TO MY GAME!!
Echo Lets begin by setting your prefered color!
:A
set choice=
set /p choice= RED, WHITE, OR BLUE?!?
if not '%choice%'=='' set '%choice%'=='Red, White, Blue'
if '%choice%'=='RED' goto RED
if '%choice%'=='WHITE' goto WHITE
if '%choice%'=='BLUE' goto BLUE
if '%choice%'=='Red' goto RED
if '%choice%'=='White' goto WHITE
if '%choice%'=='Blue' goto BLUE
if '%choice%'=='red' goto RED
if '%choice%'=='white' goto WHITE
if '%choice%'=='blue' goto BLUE
if '%choice%'=='9' goto 1Bs
echo "%choice%" is not a good color bro, do a different one
goto A
:RED
color 4
goto START SCREEN
:WHITE
color 7
goto START SCREEN
:BLUE
color 1
goto START SCREEN
:START SCREEN
cls
TITLE CHOOSER GAME BOI
Echo ---THE DND GAME---
echo Welcome to the DND game, we will first choose your Attributes
echo Strength (How hard you hit) (STR)
echo Constitution (Your health) (CNST)
echo Knowledge (Better Rolls against Vendors and Questions) (KNLG)
echo Dexterity (Your chances of dodging and Hitting) (DXT)
echo You have a total of 10 points to apply to each Attribute
echo Your points HAVE to equal 10 otherwise you will have to restart
:ATTRSET
set MXPNTS=10
set choice=
set /p STR= STR (1-10)
set choice=
set /p CNST= CNST (1-10)
set CNST=CNST
set choice=
set /p KNLG= KNLG (1-10)
set KNLG=KNLG
set choice=
set /p DXT= DXT (1-10)
set DXT=DXT
set /a ATTRTTL=STR+CNST+KNLG+DXT
echo ATTRTTL
if NOT ATTRTTL=MXPNTS goto ATTRSET
if ATTRTTL=MXPNTS goto testyay
pause
:testyay
pause
I would personally suggest:
#echo off
cls
:begin
echo HI THERE! AND WELCOME TO MY GAME!!
echo Lets begin by setting your preferred color!
:a
set /p choice= RED, WHITE, OR BLUE?!?
if "%choice%" == "" (
echo Please enter something!
cls
goto :A
)
for %%A IN (red white blue) do if /I "%choice%" == "%%A" (call :%%A & goto :start_screen)
if "%choice%" == "9" (goto 1Bs)
echo "%choice%" is not a good color bro, do a different one.
goto :a
:red
color 4
exit /b
:white
color 7
exit /b
:blue
color 1
exit /b
:start_screen
cls
title CHOOSER GAME BOI
echo ---THE DND GAME---
echo Welcome to the DND game, we will first choose your Attributes
echo Strength (How hard you hit) (STR)
echo Constitution (Your health) (CNST)
echo Knowledge (Better Rolls against Vendors and Questions) (KNLG)
echo Dexterity (Your chances of dodging and Hitting) (DXT)
echo You have a total of 10 points to apply to each Attribute
echo Your points HAVE to equal 10 otherwise you will have to restart
:ATTRSET
set "mxpnts=10"
set /p "str=STR (1-10) "
set /p "cnst=CNST (1-10) "
set /p "knlg=KNLG (1-10) "
set /p "dxt=DXT (1-10) "
set /a "attrttl=str+cnst+knlg+dxt"
echo %ATTRTTL%
if not "%attrttl%" == "%mxpnts%" (goto :attrset) else (goto :testyay)
pause
:testyay
pause
Capitalization detected! Successfully removed! As batch is a case insensitive language, uppercase letters would make noise and make reader close the tab with your question and move on - or at least me.
The whole thing about the choice variable you did was not needed. Just a for loop looping through the color words and checking (case insensitive) if the user input was red, white or blue.
I have decided to call subroutines, not goto to them to save some lines - I usually do it to my programs: you had put 3 separate commands goto START SCREEN which could be simplified calling the subroutine (which means goto to it, but then return) and then goto where you want.
Remember: it is not good for your files/folders to contain a space in their name. This can cause quite many misbehaviours. It is just the same in all languages: when you name variables, functions, subroutines or whatever, don't include spaces! I have renamed it to start_screen.
That's all, follow excellent suggestions by Squashman in comments and read the help of some commands in cmd, typing command /? and you should be fine.

Why is my batch file showing all variables

I am coding a text-based batch rpg, but the code reads all variables formatted as :VARIABLE
echo
set
if
etc.
as if it was a sequence of code.
CODE:
:start
#echo off
set /p name=Welcome to the game, what is your name?:
if %name%==ChrisPBacon goto crispy
ping localhost -n 2 >nul
cls
goto selectclass
:WARRIORSELECT1
cls
set /p question1=Your class is Warrior, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass
:PALADINSELECT1
cls
set /p question1=Your class is Paladin, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass
:MAGESELECT1
cls
set /p question1=Your class is Mage, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass
:CLERICSELECT1
cls
set /p question1=Your class is Cleric, is this correct Y/N?:
if %question1%==Y cls goto clerichealer
if %question1%==N cls goto selectclass
:selectclass
echo Welcome to the game, %name%!
ping localhost -n 2 >nul
echo Warrior
echo Paladin
echo Mage
echo Cleric
set /p CLASS=What class would you like to be?:
if %CLASS%==Warrior goto WARRIORSELECT1
if %CLASS%==Paladin goto PALADINSELECT1
if %CLASS%==Mage goto MAGESELECT1
if %CLASS%==Cleric goto CLERICSELECT1
:clerichealer
set /p surecleric=Are you sure you wish to be the Cleric? This class does 0
damage and can only heal Y/N!:
if %surecleric%==Y cls goto skillselect1
if %surecleric%==N cls goto selectclass
:skillselect1
echo Welcome to the skill selection menu, %Name% the %CLASS% From here you
can select your initial skills, with 10 points to spend at first and more
can be gained by levelling up!
echo Weight (W)
echo Attack Damage (AD)
echo Magic Damage (MD)
echo Healing Effectiveness (HE)
set /p point1 What attribute do you wish to level up with your 1st point?
if point1==W echo Increased weight by +10!
if point1==AD echo Increased Attack Damage by +20!
if point1==MD echo Increased Magic Damage by +15!
if point1==HE echo Increased Healing Effectiveness by +10 Health!
:crispy
echo Welcome Admin!
echo
echo 1) Force Delete current save
echo 2) Implement Preset save
set /p bacon=What do you wish to do?
In particular, when any class but Cleric is selected, it loops through all classes asking if this is your selected class. When Cleric is selected, it loops back to Welcome to the game, %name% etc.
The thing that makes it loop when you select cleric is that it just goes on, even when you enter "n" (instead of "N"), as SteveFest said.
But there are many issues with your code:
It doesn't account for incorrect or empty input
The if comparison is case-sensitive
no quotes for the if comparison
It's obvious it's going to get much worse with time if you keep using too many goto statements
Take a look in this snippet.
:CLERICSELECT1
cls
set /p question1=Your class is Cleric, is this correct Y/N?:
if %question1%==Y cls goto clerichealer
if %question1%==N cls goto selectclass
:selectclass
If your selection is not Y or N, it will automatically go on, and reach :selectclass. This problem occurs throughout the entire script, so fix them first.
Another thing that most new batch scripts make is the multicommand in one if statement. You could use
if "a"=="b" cls && goto blah
or
if "a"=="b" (
cls
goto blah
)
Notice the quotes " I've added, as quotes will help deal with variables with spaces.
Optionally, you can add the /i flag to if, which means case-insensitive search.
When we want to echo a blank line, we don't only use echo, instead, we use echo(, though there are some less safer methods.
Some variables are not wrapped in % signs.
Multi-line echo requires multi echos unless you want for loops
There was one missing equal sign in one of the set /p statement
Storing passwords directly in a batch file isn't safe, even after making it a .exe file, as most bat to exe converter does make a temporary bat file in the temp directory, which can be easily accessed by malicious users.

My text based game script is not working

I am making a text based rpg and the script is not working. It has something to do with the stamina. I also have a quest that is dependent on you having a certain amount of gold and that script is not working as well. I will include pictures.
:harvest
cls
echo Press 1) to harvest
set /p input17=enter:
if %input17%==1 set /a wheat= %wheat% + 5
if %input17%==1 set /a carrots= %carrots% +4
if %input17%==1 set /a stamina= %stamina% - 25 (this line)
if %stamina% < 0 goto nostamina (this line)
echo.
echo You get some wheat and some carrots.
echo.
echo check your inventory for accurate numbers.
echo.
echo Press 1) to go back.
pause >nul
goto insidehouse
:insidehouse
cls
echo You are now inside of your house.
echo.
echo Press 1) to harvest.
echo Press 2) to sell all crops.
echo Press 3) to go into your inventory.
echo Press 4) to sleep eight hours.
echo Press 5) to check for quests.
set /p input16=enter:
if %input16% EQU 1 goto harvest
if %input16% EQU 2 goto market
if %input16% EQU 3 goto Inventory1
if %input16% EQU 4 goto sleep
if %input16% EQU 5 (and) if %gold% LSS 0 goto shopping (this line)
You haven't provided much code to work with so, I can only guess at a solution.
My best guess is that you are atempting to update a variable from inside a for loop. If this is the case you need to add this line to the top of your batch file: setlocal enabledelayedexpansion. You will also need to access the affected variables like this !var! instead of this %var%.
setlocal enabledelayedexpansion causes expansion of variables to delayed in your batch file. What this will mean in the context of your program is that variables can be updated from within a for loop.

Batch roguelike errors

I am trying to make a batch roguelike but I am running in to three main problems. First of all my message system is not working. Second there is a glitch where if i try to move somewhere where I can't the choice statement keeps writing the W, S, A, and D's at the bottom. Third and most importantly there is a very annoying flashing of the screen. I tried to minimize the time it takes to load but to no avail. Here is my code:
#echo off
setlocal enableextensions
mode con: cols=54 lines=30
set num=1
set nextVar=1
set oldVar=103
set message=a
cls
echo Loading...
:startup
if not %num%==1001 (
set b%num%=.
set /a num = %num% + 1
goto startup
)
cls
set b103=#
goto update
:update
choice /c wsad /n /m ""
if %errorlevel%==1 set /a nextVar = %oldVar% - 50
if %errorlevel%==2 set /a nextVar = %oldVar% + 50
if %errorlevel%==3 set /a nextVar = %oldVar% - 1
if %errorlevel%==4 set /a nextVar = %oldVar% + 1
if defined b%nextVar% (
set b%nextVar%=#
set b%oldVar%=.
set oldVar=%nextVar%
set message="a"
goto display
)
set message="You can not move there."
goto update
:display
set ln1=%b1%%b2%%b3%...
set ln2=%b51%%b52%%b53%...
set ln3=%b101%%b102%...
...
...
...
set ln8=%b351%%b352%...
set ln18=%b851%%b852%%b853%
set ln19=%b901%%b902%%b903%%b904%...
set ln20=%b951%%b952%%b953%%b954%%b955%%b956%...%b1000%
cls
if not "%message%"=="a" (
echo %message%
goto next
)
echo.
:next
echo.
echo %ln1%
echo %ln2%
echo %ln3%
echo %ln4%
echo %ln5%
echo %ln6%
echo %ln7%
echo %ln8%
echo %ln9%
echo %ln10%
echo %ln11%
echo %ln12%
echo %ln13%
echo %ln14%
echo %ln15%
echo %ln16%
echo %ln17%
echo %ln18%
echo %ln19%
echo %ln20%
echo.
goto update
I had to ... some sections to save space. If anyone can solve these problems in particularity the screen flashing that would be super helpful. Also if anyone has ideas on generation NetHack like dungeons that would also be appreciated. Thank you.
So problems 1 and 2 are related.
First of all my message system is not working. Second there is a glitch where if i try to move somewhere where I can't the choice statement keeps writing the W, S, A, and D's at the bottom.
Change the error goto statement for the update section. You want to go to the display section so the screen is redrawn (removing the old used choice prompt) and the error message is displayed.
set message="You can not move there."
goto display
Problem 3 is a bit more difficult due to the nature of batch.
Third and most importantly there is a very annoying flashing of the screen.
The flashing is caused primarily because of one command. The cls command. Instead of just redrawing the screen it is first causing the screen to turn black before the new output is drawn causing the flicker.
Step 1: Remove the cls command. This means that you will have to output 30 lines each time to fill the screen.
Reducing the flicker even more will require a bigger overhaul on your script. Instead of echoing each line use the natural ability of the command window to wrap lines to draw the whole screen. This will reduce the amount of echo calls to a couple. Your canvas is set to 54 x 30 so that equals 1620 characters to fill the whole screen at one time.
Step 2: Output the entire screen with a single or as few calls as possible to prevent drawing/scrolling delay and use the natural line wrapping ability of the console window.
Here is an example I put together to illustrate this second step. Let me know if you have any questions.
#echo off
( if "%~1"=="" start "" /HIGH %0 1 & exit /b 1 )
setlocal enabledelayedexpansion
mode con: cols=80 lines=25
set "mark=0"
set "switch=0"
:next
if %mark% geq 920 set "mark=0" & if %switch% equ 0 ( set "switch=1" ) else set "switch=0"
set top=........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
set bot=........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
if %switch% equ 0 set top=!top:~0,%mark%!^|!top:~%mark%!
if %switch% equ 1 set bot=!bot:~0,%mark%!^|!bot:~%mark%!
goto display
:display
echo %mark%
set /p "=%top:~0,920%" <nul
set /p "=%bot:~0,920%" <nul
ping 192.0.2.2 -n 1 -w 200 >nul
set /a mark+=1
goto next
Step 3: An additional step that helps would be to increase the processes priority level. I have also added this to my example script above on the second line.

Multiple choices menu on batch file?

Hi I want to make a batch file menu, that asks 'Select app you want to install?' for example
App1
App2
App3
App4
App5
ALL Apps
Select what app:_
What I want is, for example I want to install App2, App3, and App5, so I can type on by App ID's 'Select what app:2,3,5' . And when user Select option 6, it will install all Applications!
I know this is possible on bash scripting, but Im not sure on batch scripting?
An example of batch menu is http://mintywhite.com/software-reviews/productivity-software/create-multiple-choice-menu-batchfile/
Answer
This will do what you want. Let me know if you have any questions. All you have to do is follow the two steps listed in the script.
Script
:: Hide Command and Set Scope
#echo off
setlocal EnableExtensions
:: Customize Window
title My Menu
:: Menu Options
:: Specify as many as you want, but they must be sequential from 1 with no gaps
:: Step 1. List the Application Names
set "App[1]=One"
set "App[2]=Two"
set "App[3]=Three"
set "App[4]=Four"
set "App[5]=Five"
set "App[6]=All Apps"
:: Display the Menu
set "Message="
:Menu
cls
echo.%Message%
echo.
echo. Menu Title
echo.
set "x=0"
:MenuLoop
set /a "x+=1"
if defined App[%x%] (
call echo %x%. %%App[%x%]%%
goto MenuLoop
)
echo.
:: Prompt User for Choice
:Prompt
set "Input="
set /p "Input=Select what app:"
:: Validate Input [Remove Special Characters]
if not defined Input goto Prompt
set "Input=%Input:"=%"
set "Input=%Input:^=%"
set "Input=%Input:<=%"
set "Input=%Input:>=%"
set "Input=%Input:&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
call :Validate %Input%
:: Process Input
call :Process %Input%
goto End
:Validate
set "Next=%2"
if not defined App[%1] (
set "Message=Invalid Input: %1"
goto Menu
)
if defined Next shift & goto Validate
goto :eof
:Process
set "Next=%2"
call set "App=%%App[%1]%%"
:: Run Installations
:: Specify all of the installations for each app.
:: Step 2. Match on the application names and perform the installation for each
if "%App%" EQU "One" echo Run Install for App One here
if "%App%" EQU "Two" echo Run Install for App Two here
if "%App%" EQU "Three" echo Run Install for App Three here
if "%App%" EQU "Four" echo Run Install for App Four here
if "%App%" EQU "Five" echo Run Install for App Five here
if "%App%" EQU "All Apps" (
echo Run Install for All Apps here
)
:: Prevent the command from being processed twice if listed twice.
set "App[%1]="
if defined Next shift & goto Process
goto :eof
:End
endlocal
pause >nul
you may use choice.exe see here : http://ss64.com/nt/choice.html
You want to use set /p Example below:
echo What would you like to install?
echo 1 - App1
echo 2 - App2
set /p whatapp=
if %whatapp%==1 (
codetoinstallapp1
) else if %whatapp%==2 (
codetoinstallapp2
) else (
echo invalid choice
)
Here's a trick I learned:
echo.1) first choice
echo.2) second choice
echo.3) third choice
echo.4) fourth choice
:: the choice command
set pass=
choice /c 1234 /n /m "Choose a task"
set pass=%errorlevel%
::the choices
if errorlevel 1 set goto=1
if errorlevel 2 set goto=2
if errorlevel 3 set goto=3
if errorlevel 4 set goto=4
goto %goto%
While I use only 1-4 it would be very easy to add more possible choices.
#echo off
:menu
cls
echo.
echo Select the case color you want to create:
echo ==========================================
echo.
echo App 1
echo App 2
echo App 3
echo App 4
echo.
echo ==========================================
echo Please answer Y/N to the following:
set /p App1= Install App 1?
set /p App2= Install App 2?
set /p App3= Install App 3?
set /p App4= Install App 4?
if /I "%App1%" EQU "Y" goto :Option-1
if /I "%App1%" EQU "N" goto :1
:1
if /I "%App2%" EQU "Y" goto :Option-2
if /I "%App2%" EQU "N" goto :2
:2
if /I "%App3%" EQU "Y" goto :Option-3
if /I "%App3%" EQU "N" goto :3
:3
if /I "%App4%" EQU "Y" goto :Option-4
if /I "%App4%" EQU "N" goto :End
:Option-1
App 1 Loc.
goto 1
:Option-2
App 2 Loc.
goto 2
:Option-3
App 3 Loc.
goto 2
:Option-4
App 4 Loc.
:End
Exit
Menu with analog of checkbox.
#echo off
set size=3
::preset
set chbox2=x
:prepare
for /L %%i in (0,1,%size%) do (
if defined chbox%%i (
set st%%i=Y
) else (
set chbox%%i=
)
)
:menu
cls
echo.
echo 1. [%chbox1%] name_1:
echo.
echo 2. [%chbox2%] name_2:
echo.
echo 3. [%chbox3%] name_3:
echo.
echo.
echo.
choice /C 1234567890qa /N /M "Select [1-9] >> [a]pply or [q]uit:"
echo.
set inp=%errorlevel%
if %inp%==11 (
exit
)
if %inp%==12 (
call :apply
)
::switch
if defined st%inp% (
set st%inp%=
set chbox%inp%=
) else (
set st%inp%=Y
set chbox%inp%=X
)
goto :menu
:apply
for /L %%i in (0,1,%size%) do (
if defined st%%i (
call :st%%i
echo.
)
)
echo.
pause
goto :menu
:st1
echo First Command
goto :eof
:st2
echo Second Command
goto :eof
:st3
echo Third Command
goto :eof
You can set lines checked as defaults under :preset label.
A batch file is a list of command prompt commands. The following code prints to the terminal:
echo whateveryouwant
print your menu using these echo statements in a batch file.
Getting user input can be found here: How to read input from console in a batch file?
The installing of applications is a little more complex - you need to know the requirements of your apps and where files should be moved - that should be simple, as well; use move on the appropriate files in the appropriate place.
Here's an example of a batch script menu I'm using:
#echo off
setlocal
:begin
cls
echo [LOCAL ACCOUNTS REMOTE ADMIN] --------------------------------------
echo 1 -- List local accounts on a remote machine
echo 2 -- Create a local account on a remote machine
echo 3 -- Change a local account password on a remote machine
echo 4 -- Delete a local account on a remote machine
echo;
echo 5 -- exit
echo;
set /P rmFunc="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3 4 5 x) do if #%rmFunc%==#%%I goto run%%I
goto begin
:run1
rem list local accounts code
goto begin
:run2
rem create local account code
goto begin
rem and so on, until...
:run5
:run9
:run99
:runx
endlocal
goto :EOF
The most relevant bits are the set /p line and the for...in lines. The for...in line basically compares the choice entered with every menu item number, and if match, goto run#; otherwise start over from the beginning.
I saw that none of the above answers completely answered his/her question. One feature that they have left out is selecting all the software it installs in one go (so to speak).
So I made this off the top of my head (extremely sorry if there is something wrong with it, I'll edit it if there is).
#echo off & setlocal enabledelayedexpansion
echo What would you like to install?
::Put your options here, preferably numbered.
set /p op=Type the numbers of the software you want to install (separated by commas with no spaces. E.g: 1,3,2):
for /f "delims=, tokens=1-5" %%i in ("op") do (
set i=%%i
set j=%%j
set k=%%k
set l=%%l
set m=%%m
)
if %i%X neq X set last=1b & goto %i%
:1b
if %j%X neq X set last=2b & goto %j%
:2b
if %k%X neq X set last=3b & goto %k%
:3b
if %l%X neq X set last=4b & goto %l%
:4b
if %m%X neq X set last=%m% & goto %m%
goto next
:1
::Put the code for doing the first option here
goto %last%
:2
::Put the code for doing the second option here
goto %last%
:3
::Put the code for doing the third option here
goto %last%
:4
::Put the code for doing the fourth option here
goto %last%
:5
::Put the code for doing the fifth option here
goto %last%
:next
::Put some more stuff here...
So that was a bit excessive. Feel free to change some things around and such.
What the code is doing is getting the user input (such as if you put in "1,3,4"), putting each number into its own variable, checking if that variable is empty, and if it isn't, sending you to code that does whatever the option was. It does this a few times until all the variables have been assessed.
This is a proposed analysis for improvement on David Ruhmann's code relating to the "Validate Input" section above:
Testing the menu for special characters works a charm except for the following characters "^&<". When each are submitted for input the program closes.
set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:<=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
Escaping the ^ and & works, but something very peculiar going on with the parsing of "<" and ">" (escaping these doesn't appear to work). If we reverse the order of the two statements as in the above amendment we find "<" works, but now ">" doesn't.
However, shifting the second statement with "<" down thus, both redirection characters work but now ")" doesn't!!
set "Input=%Input:"=%"
set "Input=%Input:^^=%"
set "Input=%Input:>=%"
set "Input=%Input:^&=%"
set "Input=%Input:|=%"
set "Input=%Input:(=%"
set "Input=%Input:)=%"
set "Input=%Input:<=%"
:: Equals are not allowed in variable names
set "Input=%Input:^==%"
Another great tutorial for batch menus is found here.
I'm using this
#echo off
:a
echo Welcome to a casual log-in (you are a idiot)
echo.
pause
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
set /p c=Email:
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
set /p u=Password:
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
msg * Welcome %c%.
goto a
There is actually an extremely easy way to do this.
#echo off
echo Which app do you want to install?
echo [APP 1]
echo [APP 2]
echo [APP 3]
echo.
echo Type 1, 2, or 3.
set /p "AppInstaller=>"
if %AppInstaller%==1 goto 1
if %AppInstaller%==2 goto 2
if %AppInstaller%==3 goto 3
:1
[INSTALL CODE]
:2
[INSTALL CODE]
:3
[INSTALL CODE]
The menu, when coded like this, will look like this:
Which app do you want to install?
[APP 1]
[APP 2]
[APP 3]
Type 1, 2, or 3.
>_
The code sets the variable AppInstaller to 1, 2, or 3. The file determines this and redirects you to an installer for each one.
This is fairly easy code that I use a lot in multiple choice games:
#echo off
color 0a
cls
:download
echo App 1
echo App 2
echo App 3
echo App 4
echo App 5
echo All Apps
echo
echo Select What App (1, 2, 3, ect.):
set /p apps=
if %apps%==1 goto 1
if %apps%==1 goto 2
if %apps%==1 goto 3
if %apps%==1 goto 4
if %apps%==1 goto 5
if %apps%==1 goto all
:1
(Your Code Here)
:2
(Your Code Here)
:3
(Your Code Here)
:4
(Your Code Here)
:5
(Your Code Here)
:all
(Your Code Here)

Resources