Batch File path vairiable %PATH% - batch-file

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.

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

Batch file closes after a goto command

Here's the code
#echo off
color 0a
:user_login
set /p username=What is your username?
if %username% == admin (
cls
goto admin_request_password
)
echo Incorrect username please try again.
cls
goto user_login
:admin_request_password
set /p Password=What is your Encryption Key?:
if %Password% == ecca1924889236 (
pause
cls
goto admin_menu
pause
)
echo Incorrect password
pause
cls
goto user_login
:admin_menu
echo Type acct_info for account info.
echo Type site_key for website file manager.
echo type ip_info for your ip info.
echo Type close to close the Net-Sec portal
goto admin_menu_process
:admin_menu_process
set /a choice=Waiting for Request:
if %choice% == acct_info (
cls
goto acct_info ) echo Incorrect term cls goto admin_menu
if %choice% == site_key (
goto site_key )echo Incorrect term
cls
goto admin_menu
if %choice% == ip_info (
goto ip_info ) echo Incorrect term
cls
goto admin_menu
if %choice% == close (
goto close ) echo Incorrect term
cls
goto admin_menu
:ip_info
ipconfig
pause
cls
goto admin_menu
:acct_info
echo Your username is: admin
echo your password is: ecca1924889236
pause
cls
goto admin_menu
:site_info
The username is: net-sec
The password is: netsec127
pause
cls
goto admin_menu
:close
cls
Suggestions:
Do not set a variable to %username% as that is already an existing system variable.
Enclose both strings with double quotes in a string comparison.
Use the choice command when you have a known set of choices.
Try not to include unnecessary labels.
Here is a rewrite of your code to hopefully help you in your scripting exploits:
#Echo Off
Color 0A
:getName
ClS
Set/P "uname=What is your username? "
If /I Not "%uname%"=="admin" (Echo Incorrect username please try again.
>Nul Timeout 3
GoTo getName)
:getPass
ClS
Set/P "password=What is your encryption key? "
If Not "%password%"=="ecca1924889236" (Echo Incorrect password please try again.
>Nul Timeout 3
GoTo getPass)
:menu
ClS
Echo [1] Account information.
Echo [2] Website File Manager.
Echo [3] IP information.
Echo [4] Close the Net-Sec portal.
Choice /C 1234 /M "Enter your request"
If ErrorLevel 4 GoTo close
If ErrorLevel 3 GoTo ip_info
If ErrorLevel 2 GoTo site_info
If ErrorLevel 1 GoTo acct_info
GoTo :EOF
:acct_info
Echo Your username is: admin
Echo your password is: ecca1924889236
Timeout 3
GoTo menu
:site_info
Echo The username is: net-sec
Echo The password is: netsec127
Timeout 3
GoTo menu
:ip_info
IPConfig
Timeout -1
GoTo menu
:close
ClS
Echo Closing...
Timeout 3

Multiple choice questions will respond to anything using simple cmd code when i dont want them to?

I made this code this afternoon and only started to start doing this about a day ago. I can seem to find out why when playing my game I can just press enter on my questions and it will go through to the next level even though I set questions for it.
#echo off
title 'Hospital Nightmare'
color 47
if "%1" neq "" (goto %1)
pause
:Menu
cls
echo '1 Start'
echo '2 Instructions'
echo '3 Exit'
set /p answer='Type the number of your option and press enter'
if %answer%==1 goto 'Start_1'
if %answer%==2 goto 'Instructions'
if %answer%==3 goto 'Exit'
:'Exit'
#echo off
echo Happy Dreams
pause
exit /b
:'Instructions'
#echo off
cls
echo 'Instructions'
echo.
echo Self Explanitory you
echo IDIOT!
pause
goto Menu
:'Start_1'
#echo off
cls
echo You awake in a small room
echo that smells like sterilisation
echo and surgical equiptment.
echo.
echo Even though it's small,
echo many things are inside.
echo this is strange?
echo.
echo Screams echo around you
echo and a noise is coming from
echo around the corner!
echo.
set /p answer='Press 1 to find out what it is!'
if %answer%==1 goto 'Fight_1'
:'Fight_1'
#echo off
cls
echo "YOU, YES YOU THERE! DON'T
echo LOOK AT ME LIKE IM STUPID
echo YOU KNOW WHAT I WANT, NOW
echo FIGHT ME!!"
echo.
echo What is this guy going on
echo about??
echo.
set /p answer='would you like to fight or run?'
if %answer%==Fight goto 'Fight_1'
if %answer%==Run goto 'Run_1'
:'Run_1'
#echo off
cls
echo You escape safely!
:'Fight_1'
#echo off
cls
echo "GOOD LUCK"
echo "YOU'RE GONNA NEED
echo IT!"
echo.
echo You wait for thhe first punch
echo.
set /p answer= Type the number 1 and press enter to continue:
if %answer%==1 goto 'Fight_1_Loop'
:'Fight_1_Loop'
set /a num=%random%
if %num% gtr 4 goto 'Fight_1_Loop'
if %num% lss 1 goto 'Fight_1_Loop'
if %num%==1 goto 'Lose_Fight_1'
if %num%==2 goto 'Win_Fight_1'
if %num%==3 goto 'Win_Fight_1'
if %num%==4 goto 'Win_Fight_1'
:'Lose_Fight_1'
#echo off
cls
echo "FIGHT ME EH? YOU
echo GOT NO CHANCE!"
echo.
echo You are knocked
echo out cold! :'(
pause
goto Menu
:'Win_Fight_1'
#echo off
cls
echo "I WAS ONLY JOKING
echo YOU'RE HUGE MAN
echo LEAVE ME ALONE!"
echo.
set /p answer='Would you like to save?'
if %answer%==Yes goto 'Save'
if %answer%==No goto 'Start_2'
:'Save'
#echo off
cls
echo YOU IDIOT YOU CAN'T SAVE
echo I'M NEW TO THIS AHAHAHA!
:'Start_2'
#echo off
cls
echo That guy was crazy.
echo Now where am I? and what did
echo he want with me?
echo.
echo This place is an old hospital
set /p answer='Before we keep going what's your name?'
if %answer%=="Meerkat" goto 'Hallway'
:'Hallway'
#echo off
cls
echo You turn left to into a
echo hallway to find that it
echo is very long and wide.
echo.
echo LIKE MY D*CK!!!
echo.
echo "Now is not the time to
echo joke narrator."
echo.
echo Ok
echo.
echo Well umm...
echo You turn left...
echo To find...
echo A...
echo F*CK IT! You find
echo a HOBO OCTOPUS!!!
echo.
echo "Dude wtf."
echo.
echo "Ok I'll do it."
echo.
echo YESS! Let the battle begin!
echo.
set /p answer='Will you fight this fight?'
if %answer%==Yes goto 'Fight_2'
if %answer%==No goto 'Scene_1'
:'Scene_1'
#echo off
cls
echo YOU DON'T WANT TO FIGHT
echo AN OCTOPUS?
echo.
echo WHAT ARE YOU? OCTOPUSSY?
pause
goto Menu
:'Fight_2'
#echo off
cls
You need to set the variable to blank as you get the prompt:
:Menu
cls
echo '1 Start'
echo '2 Instructions'
echo '3 Exit'
set /p answer='Type the number of your option and press enter' || SET answer=NO_ANSWER
if %answer%==NO_ANSWER GOTO Menu
if %answer%==1 goto 'Start_1'
if %answer%==2 goto 'Instructions'
if %answer%==3 goto 'Exit'
GOTO Menu
The final GOTO Menu also ensures that the value that was inserted was not just nonsense, for example sdhghdhf or some other invalid value. It will act as the "default" case.
Just copy this kind of structure to every "decision" and it should work fine.

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

Resources