i want a menu where you can change background color and foreground color - batch-file

How do I get the code below to work, or something that will do the same.
#echo off
:menu
cls
echo 1. Settings
echo.
set /p variable= Type the number of your choosing:
if %variable% equ 1 goto settings
if %variable% neq 1 goto menu
:settings
cls
echo 1. Text color
echo.
echo 2. Back
echo.
set /p variable= Type the number of your choosing:
if %variable% equ 1 goto textcolor
if %variable% neq 1 goto settings
:textcolor
cls
echo Text color
echo.
echo 1. Blue
echo.
echo 2. Gray
echo.
set /p variable= Type the number of your choosing:
if %variable% equ 1 goto backgroundcolor
if %variable% equ 2 goto backgroundcolor
if %variable% neq 1 goto textcolor
:backgroundcolor
::code to make the text the color I chose
cls
echo Background color
echo.
echo 1. Green
echo.
echo 2. Purple
echo.
echo 3. Menu
echo.
set /p variable= Type the number of your choosing:
if %variable% equ 1 goto done
if %variable% equ 2 goto done
if %variable% equ 3 goto menu
if %variable% neq 1 goto backgroundcolor
:done
::code to make the background the color I chose
goto start
:start
cls
echo Now the text is the color I chose and the background is the color I chose
pause
If you picked the wrong color and chose to go back then I dont want it to reset the background color when you chose the text color
I can get it to work but I need to do every color for each color and that is so much text and it resets.

First you could use the choice command instead of set /p for user input. You can learn more about it by using the choice /? command.
Second you should create variables to not loose the changes made. for example:
set textColor=f
set backgroundColor=0
At no point in your code did you set a color value to anything nor did you use the color command.
This should be what you wanted to do:
#echo off
set textColor=f
set backgroundColor=0
:menu
color %backgroundColor%%textColor%
cls
echo 1. Settings
echo.
choice /n /c:1
if %errorlevel% == 1 goto :settings
goto :menu
:settings
cls
echo 1. Text color
echo.
echo 2. Back
echo.
choice /n /c:12
if %errorlevel% == 2 goto :menu
if %errorlevel% == 1 goto :textcolor
goto :settings
:textcolor
cls
echo Text color
echo.
echo 1. Blue
echo.
echo 2. Gray
echo.
choice /n /c:12
if %errorlevel% == 2 set textColor=8&&goto :backroundcolor
if %errorlevel% == 1 set textColor=1&&goto :backroundcolor
goto :textcolor
:backroundcolor
cls
echo Backroundcolor
echo.
echo 1. Green
echo.
echo 2. Purple
echo.
echo 3. Menu
echo.
choice /n /c:123
if %errorlevel% == 3 goto :menu
if %errorlevel% == 2 set backgroundColor=5&&goto :start
if %errorlevel% == 1 set backgroundColor=2&&goto :start
goto :backroundcolor
:start
cls
color %backgroundColor%%textColor%
echo Now the text is the color is "%textColor%" and the backround is "%backgroundColor%"
pause
If you want to keep the text input you can use following:
#echo off
set textColor=f
set backgroundColor=0
:menu
color %backgroundColor%%textColor%
cls
echo 1. Settings
echo.
set /p variable= Type the number of your choosing:
if %variable% == 1 goto :settings
goto :menu
:settings
cls
echo 1. Text color
echo.
echo 2. Back
echo.
set /p variable= Type the number of your choosing:
if %variable% == 2 goto :menu
if %variable% == 1 goto :textcolor
goto :settings
:textcolor
cls
echo Text color
echo.
echo 1. Blue
echo.
echo 2. Gray
echo.
set /p variable= Type the number of your choosing:
if %variable% == 2 set textColor=8&&goto :backroundcolor
if %variable% == 1 set textColor=1&&goto :backroundcolor
goto :textcolor
:backroundcolor
cls
echo Backroundcolor
echo.
echo 1. Green
echo.
echo 2. Purple
echo.
echo 3. Menu
echo.
set /p variable= Type the number of your choosing:
if %variable% == 3 goto :menu
if %variable% == 2 set backgroundColor=5&&goto :start
if %variable% == 1 set backgroundColor=2&&goto :start
goto :backroundcolor
:start
cls
color %backgroundColor%%textColor%
echo Now the text is the color is "%textColor%" and the backround is "%backgroundColor%"
pause

Mmmm... This is "something that will do the same"...
#echo off
setlocal EnableDelayedExpansion
set /A textColor=15, backColor=0
set "color=0123456789ABCDEF"
:option2-3
:menu
cls
echo 1. Settings
echo X. eXit
echo/
choice /C 1X /N /M "Select an option: "
goto option1-%errorlevel%
:option1-1 Settings
echo/
echo 1. Text color
echo 2. Background color
echo 3. Reset color
echo X. eXit
echo/
choice /C 123X /N /M "Select an option: "
goto option2-%errorlevel%
:option2-1 Text color
echo/
echo/
echo Text color
echo/
for /F "delims=" %%a in ('color /? ^| findstr /B /C:" "') do echo %%a
echo/
choice /C %color% /N /M "Select text color: "
set /A "textColor=%errorlevel%-1"
goto setColor
:option2-2 Background color
echo/
echo/
echo Background color
echo/
for /F "delims=" %%a in ('color /? ^| findstr /B /C:" "') do echo %%a
echo/
choice /C %color% /N /M "Select background color: "
set /A "backColor=%errorlevel%-1"
::goto setColor
:setColor
color !color:~%backColor%,1!!color:~%textColor%,1!
goto menu
:option2-3 Reset color
color
goto menu
:option1-2 Exit
echo Exit

Related

I'm trying to create an admin "login" on batch and it just closes the cmd

I am trying to make an "admin" login and it isn't working.
#echo off
title RPG BETA
color 0b
set /p player=Welcome to Xero, what's your name?:
if %player% == Admin goto admin
:intro
echo Hello %player%!
pause
goto start
:admin
set /p password:What is the admin password?:
if %password% == Insertanypassword goto true
if %password not insertanypassword goto false
:true
cls
echo Welcome, Admin!
set /p af=Would you like to enable admin features?[y/n]:
if %af% == y goto true1
if %af% == n goto start
:true1
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=9999999
set gund=9999999
set playerh=9999999
goto fight
:start
cls
echo In the distant future, the world was on the brink of destrucion.
timeout 3 >nul
echo In the midst of the wasteland, a single man, named %player%, will overcome the odds
pause
goto BFight
:BFight
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=1
set gund=10
set playerh=20
:Fight
cls
if %CPU% leq 0 goto win
if %playerh% == 0 goto lose
cls
echo You encounter a %CPUN%!
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
echo [1]Shoot (%gund% damage) (%gun%)
echo [2]Punch (3 damage)
echo [3]Flee
set /p fp=What do you do?
if %fp% == 1 goto gun
if %fp% == 2 goto punch
if %fp% == 3 goto flee
:gun
cls
if '%gun%'=='0' goto egun
set /a gun = gun - 1
echo You fire at the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - gund
pause
goto cpufightp
:punch
cls
echo You punch the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - 3
pause
goto cpufightp
:egun
echo You have no bullets!
pause
goto Fight
:cpufightp
if %CPU% leq 0 goto win
cls
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
pause
cls
goto cpufight
:cpufight
cls
echo The %CPUN% Attacks!
timeout 4 >nul
echo It hits!
set /a playerh = playerh - CPUD
pause
goto fight
:flee
goto losef
:losef
cls echo you have fled
pause
goto suggestion
:lose
cls
echo You died
pause
cls
:win
cls
echo Congradulations, %player%! You win!
pause
cls
:suggestion
set /p suggest=What should I add to Xero?:
echo %player%: %suggest% >> xerosuggest.word
Whenever I type in "Admin" instead of going to the password screen, it just closes cmd, can anyone tell me why? i have experimented with ''s and other commands but I just can't figure it out.
The problem with respect to your specific issue is a typo.
You have:
set /p password:What is the admin password?:
Instead of:
Set /P "password=What is the admin password?: "
The doublequotes are simply good practice, your error was quite clearly that you used a : instead of a =.

Adding Variables using batch

For my AP Physics 2 project we have to create something that can calculate the equations for resistors and capacitors, I'm having problems with having the variables actually add up to a complete number rather than just being 2+4+4 as an output.
Below is my code so far:
#echo off
echo Do you want Resitors, or Capacitors.
echo Type R or C when prompted.
set /p input0=Enter:
if %input0% equ R goto R1
if %input0% equ C goto C1
goto startup
echo Press any key to continue.
pause>nul
:R1
cls
echo Do you want Series or Parallel?
echo Type S or P when prompted.
set /p input1=Enter:
if %input1% equ S goto S1
if %input1% equ P goto P1
goto Startup
echo Press any key to continue.
pause>nul
:S1
cls
echo What are your 3 values?
set /p input2=Enter:
echo.
set /p input3=Enter:
echo.
set /p input4=Enter:
echo.
set /p "Answer=%input2%+%input3%+%input4%"
echo %Answer%
goto Startup
echo Press any key to continue.
pause>nul
:P1
cls
echo What are your 3 values?
set /p input5=Enter:
echo.
set /p input6=Enter:
echo.
set /p input7=Enter:
echo.
set /a "Answer=(1/((1/%input6%)+(1/%input5%)+(1/%input7%)))"
echo %Answer%
goto Startup
echo Press any key to continue.
pause>nul

In batch my menu will not work

I was trying to create a simple menu system with batch but i can't tell if it's the 'goto' or the input part that i messed up on. help will be appreciated!
#echo off
cls
echo ==============MENU==============
echo 1.
echo 2.
echo 3.
echo choose.
set/p "menuInput"
if %menuInput%==1 (goto :1)
if %menuInput%==2 (goto :2)
if %menuInput%==3 (goto :3)
else echo error
:1 echo 1
:2 echo 2
:3 echo 3
The syntax of set /p is incorrect. Use set /? to read the help.
The parentheses around (goto :1) don't buy you anything.
The else is both syntactically incorrect (must be on the same logical line as the if) and useless (because if the if had succeeded control would have been passed to :3).
You are missing a bunch of exit /b or goto :eof in order to prevent control falling through the options.
ok i updated your code now this shold work
code here
#echo off
:menu
cls
title menu
echo ==============MENU==============
echo 1.
echo 2.
echo 3.
set /p menuInput=? 1-3
if %menuInput% EQU 1 goto 1
if %menuInput% EQU 2 goto 2
if %menuInput% EQU 3 goto 3
goto error
:1
echo1
pause
:2
echo2
pause
:3
echo3
pause
:error
echo error press 1-3
pause>nul
goto menu
As mentioned by #AlexP,your syntax of set /p is incorrect
you need to use set < space > /p < space > userinput=
and not set/p and there should be = sign after your variable name
SET /P variable=[promptString]
Read More about using SET & IF ELSE and also CHOICE
(edit- removed misleading code for error check)
sample batch with menu using Choice and SET /P IF Else while choosing option from menu.
Example 1 using IF ELSE (input error check)
#echo off
::Your Menu to choose from
:menu
cls
echo ==============MENU==============
echo 1.command menu item 1
echo 2.command menu item 2
echo 3.command menu item 3
echo.
set /p userinput=
if %userinput%==1 (
goto 1
) else if %userinput%==2 (
goto 2
) else if %userinput%==3 (
goto 3
) else (
goto error
)
:1
echo command 1
pause>nul
goto menu
:2
echo command 2
pause>nul
goto menu
:3
echo command 3
pause>nul
goto menu
::Incorrect Input go back to menu
:error
echo Incorrect User Input
pause>nul
goto menu
:end
exit
Example 2 using CHOICE command (no error input check)
#echo off
::Your Menu to choose from
:menu
cls
echo ==============MENU==============
echo 1.command menu item 1
echo 2.command menu item 2
echo 3.command menu item 3
::check and limit user input to selection
choice /C 123 /N /M "Your Selection"
IF "%ERRORLEVEL%"=="1" goto 1
IF "%ERRORLEVEL%"=="2" goto 2
IF "%ERRORLEVEL%"=="3" goto 3
:1
echo command 1
pause>nul
goto menu
:2
echo command 2
pause>nul
goto menu
:3
echo command 3
pause>nul
goto menu
Wrong is set /p.
try set /p menuInput =

What is wrong with this IF NOT statement?

This code is supposed to make it so that if I press 1 - 4 it would go the the options 1 - 4 and if i press something else it would say that its not an option. But if I type something else it goes to debug anyway.
And I've checked the code several times and I can't find an error. I even compared it to a code like this that actually worked and I even Ctrl-c Ctrl-v'ed it and it still won't work.
:admin
cls
color %debug%
echo You have accessed the admin debug menu
echo Do what you wan't
echo [1] Set Debug color
echo [2] Set Default color
echo [3] Set Warning color
echo [4] Exit
set /p "lol=> "
IF /i %lol%==1 goto dbg if NOT goto 9
IF /i %lol%==2 goto dfc if NOT goto 9
IF /i %lol%==3 goto wnc if NOT goto 9
IF /i %lol%==4 goto start if NOT goto 9
:dbg
cls
color %debug%
echo Set Debug color
set /p "debug=> "
echo Debug color set to %debug%
color %debug%
pause
goto admin
:dfc
cls
color %debug%
echo Set Default color
set /p "default=> "
echo Default color set to %default%
pause
goto admin
:wnc
cls
color %debug%
echo Set Warning color
set /p "warning=> "
echo Warning color set to %warning%
pause
goto admin
:9
cls
color %warning%
echo This is not a viable option!
ping localhost -n 5 >nul
goto admin
I test it out and when i type ex 5 it still goes to debug.
You should take a closer look at how IF statements work.
E.g. this line IF /i %lol%==1 goto dbg if NOT goto 9 makes absolutely no sense.
Your code should look something like this:
...
set /p "lol=> "
IF /i "%lol%"=="1" goto dbg
IF /i "%lol%"=="2" goto dfc
IF /i "%lol%"=="3" goto wnc
IF /i "%lol%"=="4" goto start
cls
color %warning%
echo This is not a viable option!
ping localhost -n 5 >nul
goto admin
...
IF /i %lol%==4 (goto start) else goto 9
Although it's normal practise to use
IF /i "%lol%"=="4" (goto start) else goto 9
which provides some protection against no entry or an entry containing spaces.
try this code
IF /i %lol%==1
(goto dbg)
IF /i %lol%==2
(goto dfc)
IF /i %lol%==3
(goto wnc)
IF /i %lol%==4
(goto start)
else
(goto 9)

batch command input menu

i have a question . It is possible to make a batch menu to accept multiple commands at the same time?
Example : ( my code )
#ECHO OFF
set tries=6
:top
cls
set /a tries=%tries% -1
if %tries%==0 (
goto penalty
)
Echo You have %tries% attempts left.
Echo Please enter your password to proceed
set /p password=
if %password%==Parola ta (
echo Welcome Your Name
ping localhost -n 5 >nul
cls
Echo CONNECTED!
C:
CD\
CLS
:MENU
CLS
ECHO ============= MENU NAME =============
ECHO -------------------------------------
ECHO 1. System Information TXT file
ECHO 2. Selection 2
ECHO 3. Selection 3
ECHO 4. Selection 4
ECHO 5. Selection 5
ECHO 6. Selection 6
ECHO 7. Selection 7
ECHO -------------------------------------
ECHO 8. Selection 8
ECHO -------------------------------------
ECHO 9. Selection 9
ECHO -------------------------------------
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.
SET INPUT=
SET /P INPUT=Please select a number:
IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='4' GOTO Selection4
IF /I '%INPUT%'=='5' GOTO Selection5
IF /I '%INPUT%'=='6' GOTO Selection6
IF /I '%INPUT%'=='7' GOTO Selection7
IF /I '%INPUT%'=='8' GOTO Selection8
IF /I '%INPUT%'=='9' GOTO Selection9
IF /I '%INPUT%'=='Q' GOTO Quit
CLS
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection1
systeminfo.exe>systeminfo.txt
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection2
Call cleanup.bat
:Selection3
and in here too...
:Selection4
and so on
:Selection5
and so on
:Selection6
and so on
:Selection7
and so on
:Selection8
and so on
:Selection9
and so on
:Quit
CLS
ECHO ==============THANKYOU===============
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE>NUL
EXIT
pause
cls
) else (
goto top
)
goto top
How to make the program accept more than 1 command?
For example 1,3,5 to execute in the same time?
or another question, how its possible to undo .exe back to .bat?
there such a program?
Suggestion: make call from goto and concatenate the commands with &:
IF /I '%INPUT%'=='1' GOTO:Selection1
IF /I '%INPUT%'=='2' CALL:Selection2&CALL:Selection2&CALL:Selection4
IF /I '%INPUT%'=='3' CALL:Selection3&CALL:Selection3
IF /I '%INPUT%'=='4' CALL:Selection4&CALL:Selection4
IF /I '%INPUT%'=='5' CALL:Selection5
IF /I '%INPUT%'=='6' CALL:Selection6
IF /I '%INPUT%'=='7' CALL:Selection7
IF /I '%INPUT%'=='8' CALL:Selection8
IF /I '%INPUT%'=='9' CALL:Selection9
IF /I '%INPUT%'=='Q' GOTO:Quit
To get this work you must alse add goto:eof (eof=end of file) after the jump labels, eg.:
:Selection2
Call cleanup.bat
goto:eof
:Selection3
and in here too...
goto:eof
:Selection4
and so on
goto:eof
:Selection5
and so on
goto:eof
...
..
.
goto:eof returns control to the main program.
Is this what you were looking for?
File Menu.cmd
#echo off
setlocal
set quit=false
set /p InputChoices=Enter Choice(s) (A,B,C)
echo %InputChoices%
call :executeChoices %InputChoices%
endlocal
goto :eof
:executeChoices
if [%1]==[] goto :eof
call :Step%1
shift
goto :executeChoices
goto :eof
:StepA
echo Step A
goto :eof
:StepB
echo Step B
goto :eof
:StepC
echo Step C
goto :eof
Works like this:
c:\>Menu.cmd
Enter Choice(s) (A,B,C) b c a
b c a
Step B
Step C
Step A
You can use the start "" command keyword and each command will run in it's own window, but initiated by your menu batch file.

Resources