Adding Variables using batch - batch-file

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

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 =.

Issue with Batch file input for "War Games" game

If you are familiar with the 1983 movie "War Games" you should remember the part where the computer ask him "Shall We Play a Game". I have been trying to recreate that and this is what I have so far. The issue is after I enter the password the Command Prompt window closes out. Can someone help me find my mistake?
#echo off
title Shall We Play a Game?
color 0b
set /a tries=3
set password=Joshua
:top
echo %tries% Tries Remaining
set /p pass=Password:
if %pass%==%password% (
goto correct
)
set /a tries=%tries -1
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo CONNECTION TERMINATED
pause
exit
:correct
goto greeting
:greeting
echo Shall we play a game?
echo y/n
set input=
if %input%=y goto y
if %input%=n goto n
:y
echo How about
echo Chess
echo Tic-Tac-Toe
echo Snake
echo Global Thermonuclear War
if %opt%==Chess goto Chess
if %opt%==Tic-Tac-Toe goto TicTacToe
if %opt%==Snake goto Snake
if %opt%==Global Thermonuclear War goto Global Thermonuclear War
:n
echo Thats too bad! Maybe we should play some other day!
pause
exit
:chess
:tictactoe
echo Are you sure?
echo y/n
set response=
if %response%==y goto tictactoe1
if %response%==n goto tictactoe2
:tictactoe1
echo Go Back?
echo y/n
set feedback=
if %feedback%==y goto greeting
if %feedback%==n goto tictactoe2
:tictactoe2
echo testing
goto tictactoe2
This should be what you're trying to do:
#echo off
title Shall We Play a Game?
color 0b
set /a "tries=3"
set "password=Joshua"
:top
echo %tries% Tries Remaining
set /p "pass=Password: "
if "%pass%"=="%password%" (
goto correct
)
set /a "tries=%tries% -1"
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo CONNECTION TERMINATED
pause
exit
:correct
goto greeting
:greeting
echo Shall we play a game?
echo y/n
set /p "input="
if "%input%"=="y" goto y
if "%input%"=="n" goto n
:y
echo How about
echo Chess
echo Tic-Tac-Toe
echo Snake
echo Global Thermonuclear War
set /P "opt="
if "%opt%"=="Chess" goto Chess
if "%opt%"=="Tic-Tac-Toe" goto TicTacToe
if "%opt%"=="Snake" goto Snake
if "%opt%"=="Global Thermonuclear War" goto GlobalThermonuclearWar
:n
echo Thats too bad! Maybe we should play some other day!
pause
exit
:chess
:tictactoe
echo Are you sure?
echo y/n
set /p response=
if %response%==y goto tictactoe1
if %response%==n goto tictactoe2
:tictactoe1
echo Go Back?
echo y/n
set /p feedback=
if %feedback%==y goto greeting
if %feedback%==n goto tictactoe2
:tictactoe2
echo testing
goto tictactoe2
You forgot /P in a set a couple of times, this is used to get user input. In your set /a tries=%tries -1 you also forgot to put a second % around tries, this should be set /a tries=%tries% -1. Furthermore, you should put "" double quotes around your variables if you're comparing them, this prevents the script from braking if a variable doesn't exist or is empty. You also shouldn't have spaces in your labels, and you should put quotes around your set, like this: set "variable=value", this prevents trailing spaces from getting in your variables

Batch file closing instead using "goto" command

My code is not completely finished but it was working.Now it closes after the first section instead of going to section two.I know that this is probably a dumb mistake but I would appreciate the help!(The lines are just for organization)
#echo off
title First batch file
color 1f
::-----------------------
----------
:one
cls
echo Please enter your
name below.
echo.
echo.
set /p name=Name:
echo.
echo.
echo Hello %name%!
echo.
echo.
pause >nul
goto two
::-----------------------
----------
:two
cls
echo Would you like to
create a new account?
echo (yes or no)
echo.
echo.
set /p yorn=>>
if yorn equ yes then goto
new
if yorn equ no then goto
login
pause >nul
::-----------------------
---------
:new
cls
echo Please enter your
user name.
echo.
echo.
set /p username=Username:
cls
echo.
echo Please enter your
password.
echo.
echo.
set /p password=Password:
cls
echo.
echo Please confirm your
password.
echo.
echo.
set /p cpassword=Confirm
password:
cls
echo.
echo.
if %password% equ
%cpassword% goto
confirmscreen
if %password% new
%cpassword% goto new
pause >nul
:------------------------
---------
:confirmscreen
cls
echo Confirm:
echo.
echo.
echo Username: %username%
echo.
echo Password: %password%
echo.
echo.
set /p yorn2=Yes or No?:
if yorn2 equ yes goto
login
if yorn2 equ no goto new
pause >nul
:------------------------
---------
#echo off
title First batch file
color 1f
::---------------------------------
:one
cls
echo Please enter your name below.
echo.
echo.
set /p name=Name:
echo.
echo.
echo Hello %name%!
echo.
echo.
pause >nul
goto two
::---------------------------------
:two
set /p yorn=Would you like to create a new account? (yes or no) :
if %yorn% equ yes then goto new
if %yorn% equ no then goto login
goto:two

Batch File path vairiable %PATH%

I have been creating a chat room for my school, but I have to bring home the file to make changes as they become needed but my problem is the file path has to be changed each time I move the files from one system to another. so I would like to know how to create a %PATH% that will work for me
This is my full code for the main file:
As you may notice I'm new to this
NOTE: Everything here works fine with a set file path but I want it to work easier for when I change computers.
Anything could probably help
#echo off
:Tittle
cls
color 74
title Terms of Service
echo _________________________-Terms-______________________________
echo If you are using this ChatRoom then you agree to the following.
echo *you will not use Horrid Language
echo *you will make your account with either your real name or
echo Student id
echo.
echo This is monitored everyday so if anything is out of line it will be removed.
echo.
echo If you agree to follow these terms then type "yes" otherwise exit.
set /p c=Do you Agree to follow the terms?:
if %c% EQU yes goto Menu
if %c% EQU ADMIN1423 goto Admin
if %c% EQU dad goto Menu
if %c% EQU carrie goto Menu
if %c% EQU dad SET PATH=%PATH%;c:\Users\Dan W Frye\Desktop\(-_-)
:Admin
color 02
cls
Echo
Echo.
Echo 1.) Check User list
Echo.
Echo 2.) Create File Path
Echo.
Echo 3.) Admin Help
Echo.
echo 4.) N/A
echo.
Echo
set /p c=Selection Number:
if %c% EQU 1 goto UserList
if %c% EQU 2 goto CreatePath
if %c% EQU 3 goto AdminHelp
if %c% EQU 4 goto Admin
if %c% EQU back goto Tittle
:UserList
color 0b
cls
title User Listing
cls
start cmd
CALL "%PATH%\Data\Chat Settings\Users\BuAsTeCrHs.bat"
pause
goto Admin
:CreatePath
cls
color 01
echo
echo.
echo The File Path Must look like this(No " "): "Driver (C:)"\Containing folder" thats it the echo rest is automatically "\Data\Chat Settings\Users\....."
echo.
echo
echo.
echo The File Path you want to create.
set /p PATH=File Path:
echo.
echo The Location/Device you are using.
set /p LOC=Location:
echo.
echo %PATH% >>"%PATH%\Data\Chat Settings\File Paths\%LOC%.txt"
echo The file path has been created!
pause
goto Admin
:AdminHelp
pause
goto Tittle
:Menu
color 0b
cls
Echo -[ChatBox]-
Echo
Echo.
Echo 1.) Login
Echo.
Echo 2.) Register
Echo.
Echo 3.) Exit
Echo.
echo 4.) Help
echo.
Echo
Echo.
set /p c=Selection Number:
if %c% EQU 1 goto Login
if %c% EQU 2 goto Register
if %c% EQU 3 exit
if %c% EQU 4 goto help
if %c% EQU 5 goto Terms
:help
cls
echo if you are not able to see the chat log then you must not have
echo the file "Chatroom_reader.bat" open without this you cannot see
echo messages sent by other users.
echo.
echo if you have suggestions or comments then please type "comment"
echo.
echo if you need assistance with any other problem you may have
echo encountered then please type "other" to let the developer know
echo what the problem is. otherwise type "back" to go back to the menu.
set /p c=Option:
if %c% EQU comment goto comments
if %c% EQU other goto other
if %c% EQU back goto menu
:comments
cls
title Comments
echo Enter your Username, and Password to Place a Comment
echo.
set /p UN=Username:
echo.
set /p PW=Password:
echo.
if NOT Exist "%PATH%\Data\Chat Settings\Users\%UN%.txt" Goto Failed
echo %PW% >"%tmp%\chat.tmp"
fc "%tmp%\chat.tmp" "%PATH%\Data\Chat Settings\Users\%UN%.txt" >nul
if errorlevel==1 goto Failed
if errorlevel==0 goto Comment
:Comment
cls
echo.
set /p SUBJECT=Subject:
echo.
set /p COMMENT=Comment:
echo.
echo %SUBJECT% : %COMMENT% >"%PATH%\Data\Chat Settings\Comments\%UN%.txt"
goto User
:other
cls
title Other
echo Enter your Username, and Password to Place a Comment
echo.
set /p UN=Username:
echo.
set /p PW=Password:
echo.
if NOT Exist "C:\Users\Dan W Frye\Desktop\(-_-)\Data\Chat Settings\Users\%UN%.txt" Goto Failed
echo %PW% >"%tmp%\chat.tmp"
fc "%tmp%\chat.tmp" "C:\Users\Dan W Frye\Desktop\(-_-)\Data\Chat Settings\Users\%UN%.txt" >nul
if errorlevel==1 goto Failed
if errorlevel==0 goto OtherA
:OtherA
cls
set /p OTHERC=Other Concern:
echo.
echo %OTHERC% >"%PATH%\Data\Chat Settings\Other\%UN%.txt"
goto User
:Login
cls
echo Enter your Username, and Password to login to the Chat Server
echo.
set /p UN=Username:
echo.
set /p PW=Password:
echo.
if NOT Exist "%PATH%\Data\Chat Settings\Users\%UN%.txt" Goto Failed
echo %PW% >"%tmp%\chat.tmp"
fc "%tmp%\chat.tmp" "%PATH%\Data\Chat Settings\Users\%UN%.txt" >nul
if errorlevel==1 goto Failed
if errorlevel==0 goto User
:User
cls
Echo Welcome %UN% The Current date is %date%
echo
echo.
echo 1.) Chat
echo.
echo 2.) Logout
echo.
echo 3.) Change Password
echo.
echo 4.) Private Chat
echo.
echo 5.) Enter a Private Chat room
echo.
echo
set /p c=Selection Number:
if %c% EQU 1 goto chat
if %c% EQU 2 goto Menu
if %c% EQU 3 goto CHP
if %c% EQU 4 goto PRIVATE
if %c% EQU 5 goto PRIVATENTER
:PRIVATENTER
echo Please enter the name of the Private chat room if you do not know the name you may not enter.
set /p Chat=
if %Chat% EQU scooter goto scooter
if %Chat% EQU Cre-Br goto Cre-Br
:Cre-Br
cls
set name=[%time%]%UN%
cls
color 02
echo Last Message sent by %UN% \/
echo [%time%]%UN%:%text%
set /p text=Say:
echo %name% : %text% >>"%PATH%\Data\Chat Settings\Program_Files\Cre-Br.txt"
goto Cre-Br
:scooter
cls
echo %Chat%
set name=[%time%]%UN%
color 02
echo Last Message sent by %UN% \/
echo [%time%]%UN%:%text%
set /p text=Say:
echo %name% : %text% >>"%PATH%\Data\Chat Settings\Program_Files\scooter.txt"
goto scooter
:PRIVATE
cls
set /p Chat=Chat Name:
echo this is %UN%s Private chat room >>"%PATH%\Data\Chat Settings\Program_Files\%Chat%.txt"
echo.
echo #echo off >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo color 0b >>"%PATH%H:\(-_-)\Data\Chat Settings\Private Chats\%Chat%.bat"
echo cls >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo title Message Box >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo :home >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo cls >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo findstr /v "g91dhjt637hsuexv27niw9" "%PATH%\Data\Chat Settings\Program_Files\%Chat%.txt" >>"C:\Users\Dan W Frye\Desktop\Batch\Chat Settings\Private Chats\%Chat%.bat"
echo goto home >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
goto User
:CHP
cls
set /p PW=Old Password:
echo.
set /p NP=New Password:
echo %NP% >"%PATH%\Data\Chat Settings\Users\%UN%.txt
goto User
:Register
cls
color 07
echo Register (Note the username is your screen name Please use your real name or School ID EX, CaBu56789)
echo.
set /p NU=Username:
echo.
set /p NP=Password:
echo.
echo %NP% >"%PATH%\Data\Chat Settings\Users\%NU%.txt"
echo.
cls
goto login
:Failed
color 0c
cls
echo You have entered am invalid Username and or Password
echo Please try again or Register for free
pause
goto menu
:chat
set name=[%time%]%UN%
cls
color 02
echo Everything said here is on recored please mind your
echo language!
echo Last Message sent by %UN% \/
echo [%time%]%UN%:%text%
set /p text=Say:
echo %name% : %text% >>"%PATH%\Data\Chat Settings\Program_Files\ChatRoom.txt"
goto chat
%PATH% is reserved environment variable, documented here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true
For diagnostics purposes you could echo that variable to analyze if that has such value you intended to have:
echo %path%
As mentioned by #Jermu Virtanen the %PATH% variable is a windows system set path...you should not use/change it unless you really know what you are doing.
So instead of using %PATH% use something like %PROGPATH%.
Also as mentioned by #foxdrive, if the user enters "dad" it will go to "menu" before setting the path variable. And again if you instead chose 'Admin' then to 'List users' it will again be calling the path without first setting it.
You could automate some of the login or path settings by grabbing the computer's domain name %userdomain% and current user %username%.
Ie;
SET "progpath=c:\Users\%username%\Desktop\(-_-)"
In your code the path was being set after it had already branched to the menu. This may work for you:
if %c% EQU carrie goto Menu
if %c% EQU dad SET PATH=%PATH%;c:\Users\Dan W Frye\Desktop\(-_-)& goto menu
To put the batch file on the path for every user, use this:
set path=%path%;%PUBLIC%
and copy the batch file to C:\Users\Public and run it from there.
Unless it needs extra permissions it should run from there for every user.

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