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

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

Related

Batch variables do not work

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

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

How to make this BATch file have a timer (time left to shutdown or reset)?

How to make this BATch file have a timer?
Would like to see how much time left. Windows only says "Windows will shut down in less then a minute".
#echo off
color 0A
title Restart\/Shutdown
:start
echo Welcome, %USERNAME%
echo What would you like to do?
echo.
echo 1. Shut down
echo 2. Restart
echo.
echo 0. Quit
echo.
set /p choice="Enter your choice: "
if "%choice%"=="1" goto shutdown
if "%choice%"=="2" goto restart
if "%choice%"=="0" exit
echo Invalid choice: %choice%
echo.
pause
cls
goto start
:shutdown
shutdown.exe -s -t 10
:restart
shutdown.exe -r -t 10
You could use the command "timeout /t 10" or something like:
#ECHO OFF
FOR /L %%# IN (10,-1,1) DO (SET/P "=%%# seconds left... "<NUL:
PING -n 2 127.0.0.1 >NUL
CLS:)
#ECHO off
cls
color 0B
:taskkill
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Program To Shutdown
echo.
set /p taskkill=
if "%taskkill%" == "%taskkill%" goto taskkillconfirm
exit
:taskkillconfirm
color 0B
:image
set image=/im
if "%image%" == "%image%" goto imageconfirm
exit
:imageconfirm
color 0B
:forced
set forced=/f
if "%forced%" == "%forced%" goto forcedconfirm
exit
:forcedconfirm
:hour
color 0B
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo. Hours?:
echo.
set /p hour=
:min
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Minutes?:
echo.
set /p min=
:sec
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo Seconds?:
echo.
set /p sec=
:continue
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo %taskkill% Shutdown in
echo.
echo %hour% Hours %min% Minutes %sec% Seconds
set /a sec="%sec%-1"
if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto done
ping -n 2 127.0.0.1 >NUL
goto continue
:done
color 0B
cls
taskkill %image% %taskkill% %forced%
exit
" ping localhost -n 10 >nul " take off quotations and to edit the amount of seconds replace the 10 with whatever you

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