Batch variables do not work - batch-file

I have the following batch script:
#echo off
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar %argi% %argii%
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit
The problem is that if I run it on the first time, the java jar file is throwing exceptions because it doesn't understands the variable arguments %argi% and %argii%. I press [b] to start again at the options menu, I do the same thing again and voila, it works! Why? Why it doesn't work at the first time, and if i press [b] to go back and put again the args it works fine every time?
I start the app:
If I kill the script with Ctrl-C and reopen it, it will have the same behaviour again...first time error, and then success.
Thank you!

you need delayed expansion :
#echo off
setlocal enableDelayedExpansion
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
echo Give the parameters:
set /p argi=
set /p argii=
START /B java -jar JavaClient.jar !argi! !argii!
)
if %udefine%==e goto exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit

Related

How do I start Minecraft and Valorant from a batch file?

Last night, I decided to try coding a batch file. I used some references and some online walkthroughs and ended up making the following program.
echo off
Title Log into CMD
:home
color 0A
echo ==================
echo.
echo [1] Log In
echo [2] Exit
set /p login=
if %login%==1 goto 1
if %login%==2 goto 2
goto error
:1
echo.
echo ==================
echo.
set and=if
set /p name=Enter your name:
set /p pass=Enter your password:
if %name%==Bob %and% %pass%==***** echo Welcome, Omid
if not %name%==Bob goto no
if not %pass%==***** goto no
goto GameMenu
:no
echo You're not Bob!
pause
echo Goodbye!
pause
exit
:2
exit
:error
echo You have to select a valid option!
:GameMenu
Title Game Menu
echo.
echo ==================
echo.
echo [1] Minecraft
echo [2] krunker.io
echo [3] Valorant
set /p gc=Enter the number of the game you would like to play:
if %gc%==1 goto Minecraft
if %gc%==2 goto krunker
if%gc%==3 goto Valorant
goto error
:Minecraft
start MinecraftLauncher.exe
exit
:krunker
start https://www.krunker.io/
exit
:Valorant
start RiotClientServices.exe
exit
This program is a simple one that you log into with a username and password, which then leads to a directory of games to open. The krunker.io works, but I wasn't sure on how to call Minecraft and Valorant. The processes that open up the games are what I called. For example, MinecraftLauncher.exe is the name of the process that would launch Minecraft. When I choose Minecraft from the menu, it gives the error message 'Windows cannot find "MinecraftLauncher.exe" Make sure you typed the name correctly, and then try again.` This is strange given this is the literal name of the launcher. I was wondering if anyone with a bit more experience could help me out.
You have to go thru the path to initialize the application. Here's a step-by-step:
cd/
cd "Program Files (x86)/Minecraft"
start MinecraftLauncher.exe
You have to ensure that cmd is at c:\ using cd/, then go to launcher file location and starts it. You can do that by using start/d.
So, here's your code:
#echo off
Title Log into CMD
:home
color 0A
echo ==================
echo.
echo [1] Log In
echo [2] Exit
set /p login=
if %login%==1 goto 1
if %login%==2 goto 2
goto error
:1
echo.
echo ==================
echo.
set and=if
set /p name=Enter your name:
set /p pass=Enter your password:
if %name%==Bob %and% %pass%==***** echo Welcome, Omid
if not %name%==Bob goto no
if not %pass%==***** goto no
goto GameMenu
:no
echo You're not Bob!
pause
echo Goodbye!
pause
exit
:2
exit
:error
echo You have to select a valid option!
:GameMenu
Title Game Menu
echo.
echo ==================
echo.
echo [1] Minecraft
echo [2] krunker.io
echo [3] Valorant
set /p gc=Enter the number of the game you would like to play:
if %gc%==1 goto Minecraft
if %gc%==2 goto krunker
if%gc%==3 goto Valorant
goto error
:Minecraft
cd/
start/d "C:\Program Files (x86)\Minecraft" MinecraftLauncher.exe
exit
:krunker
start https://www.krunker.io/
exit
:Valorant
cd/
start/d "C:\Program Files (x86)\*VALORANT LAUNCHER FOLDER*" RiotClientServices.exe
exit
And your login system is broken. If you put any key other than 1 or 2, you go direct to :GameMenu, it can be easily solved by putting :error at the very end of the file or using a goto home before echo You have to select a valid option!
Hope this helps you.

I am trying to make my own Batch Game, but when I try to run the Script/Code it Instantly Closes

Alright, so I am trying to make a batch game. I have already coded/scripted the menu. So I tried to run it to see if it works, but it instantly closes! could anyone take a look at the code/script to help me find out what I am doing wrong?
Also I am using Notepad++
#echo off
title Dungeon Slayer - A Text RPG
:MainMenu
cls
echo.
echo Dungeon Slayer
echo A Text RPG
echo.
echo Build: A1
echo.
echo 1. Play
echo.
echo 2. Settings
echo.
echo 3. Changelog
echo.
echo 4. Exit
echo.
set /p MainMenuSelection
if /p "%MainMenuSelection%" EQU "1" goto :Play
if /p "%MainMenuSelection%" EQU "2" goto :Settings
if /p "%MainMenuSelection%" EQU "3" goto :Changelog
if /p "%MainMenuSelection%" EQU "4" goto :Exit
goto :MainMenu
:Play
cls
echo.
echo Currently Gameplay Development has not been started.
echo.
pause
goto :MainMenu
:Settings
cls
echo.
echo Currently we have not made any settings.
echo.
pause
goto :MainMenu
:Changelog
cls
echo.
echo 11/16/2018
echo.
echo -A1 Build Released
echo.
pause
goto :MainMenu
:Exit
cls
exit
Here's an example using Choice.
#Echo Off
Set "Build=A1"
Set "BDate=11/16/2018"
Set "GName=Dungeon Slayer"
Set "GDesc=A Text RPG"
Title %GName% - %GDesc%
:MainMenu
ClS
Echo=
Echo=%GName%
Echo=%GDesc%
Echo=
Echo Build: %Build%
Echo=
Echo 1. Play
Echo=
Echo 2. Settings
Echo=
Echo 3. Changelog
Echo=
Echo 4. Exit
Echo=
Choice /C 1234 /M "Which item"
If ErrorLevel 4 Exit /B
If ErrorLevel 3 GoTo ChangeLog
If ErrorLevel 2 GoTo Settings
:Play
ClS
Echo=
Echo Sorry, gameplay development has not yet started.
Echo=
Timeout 3 >Nul
GoTo MainMenu
:Settings
ClS
Echo=
Echo Sorry, we are yet to create any settings.
Echo=
Timeout 3 >Nul
GoTo MainMenu
:ChangeLog
ClS
Echo=
Echo=%BDate%
Echo=
Echo -%Build% Build Released
Echo=
Timeout 3 >Nul
GoTo MainMenu

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

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.

Resources