incorrect syntax command in batch file - batch-file

i'm new to the coding of batch files and got an error wich i can not solve..
the problem occurs at the :secName , I made a printscreen at the flash(just before it exits cmd), and it said: "so your name is [what %name% is]"and then "the syntax of the command is incorrect.
goto was unexpected at this time."
i've got this coding done now:
#echo off
goto Welcome
title The Game Of Everything
:welcome
echo.
echo Welcome!
echo What is your name?
set /p name=Enter:
goto secName
:secName
cls
echo.
echo so your name is %name%?
set /p secName:yes or no?:
if %secName%== yes goto begin
if %secName%==no goto welcome
:begin
cls
echo.
echo Welcome %name%, to "The Game Of Everything"!
echo I hope you enjoy this mini-game, downloadable on your computer!
echo.
echo It isn't the prettiest, but who gives a toss right?
echo press any key to continue...
pause>nul
exit

change this:
set /p secName:yes or no?:
to this
set /p secName=yes or no?:

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.

Confusion with Batch-file code

So recently I started learning Batch-file, and today I decided to try and make a game with it. I have a few errors, and can't figure out what is wrong with the code.
Heres the code:
#echo off
title Batch Rpg
color 07
echo Welcome to Batch Rpg!
echo.
echo.
pause
:menu
cls
echo.
echo -Menu-
echo.
echo.
echo.
echo 1) Begin
echo.
echo 2) How to play
echo.
echo 3) Exit
echo.
echo.
echo.
echo ---------
echo.
set /p c=Choice:
if %c%==1 goto prestart1
if %c%==2 goto howtoplay
if %c%==3 goto cfr_exit
if NOT %c%==1 if NOT %c%==2 if NOT %c%==3 goto menu
:cfr_exit
cls
echo.
echo Are you sure you want to exit?
echo.
set /p c=(Y/N):
if %c%=="Y" exit
if %c%=="N" goto menu
if NOT %c%=="Y" if NOT %c%=="N" goto cfr_exit2
:cfr_exit2
cls
echo.
echo You must enter a valid option.
echo.
pause
goto cfr_exit
:howtoplay
cls
echo.
echo -How to play-
echo.
echo.
echo.
echo This game is very simple. There will be a number with an option after it, type the option in and it will perform an action(As the option would say).
echo.
pause
goto menu
:prestart1
cls
echo.
echo Welcome to land of Fageryth!
echo.
echo What is your name, adventurer?
echo.
set /p playername=Name:
goto prestart2
:prestart2
cls
echo.
echo What would be your more valued statistic, Attack damage, or Hit points?
echo.
echo.
echo.
echo 1)Attack damage(Atk)
echo.
echo 2)Hit points(Hp)
echo.
echo.
echo.
echo ---------
echo.
set /p playermorevaluedstat=Choice:
if %playermorevaluedstat%==1
set playeratk=6
set playerhp=25
if %playermorevaluedstat%==2
set playeratk=4
set playerhp=30
if NOT %playermorevaluedstat%==1 if NOT %playermorevaluedstat%==2 goto prestart2
cls
echo playeratk
echo playerhp
pause
I'm having trouble with the :prestart2 section of my code. With the end of it, I tried to make it where if the variable wasn't equal to 1 or 2, which were the options, then it send the player back up to the start of the section again, and also, when it finishes checking, I'm trying to make it display the two variables playeratk and playerhp but instead it just closes out. I am really lost here and would appreciate the help!
A couple things before we start:
First, when troubleshooting your batch scripts, get rid of (comment out) your echo off and cls lines so that you can see where it's going wrong.
Second, you should always double-quote your variables to make sure you're not inadvertently including spaces when setting and comparing them. It doesn't seem to actually be causing problems in your code, but it's a good habit to get into:
set "var1=something"
set "var2=a string with spaces"
if "%var1%"=="something" echo %var1%
Now, the problem in your code is with the two if statements stretching over multiple lines. If you're going to do that, you have to put them inside of parentheses.
set /p playermorevaluedstat=Choice:
if %playermorevaluedstat%==1 (
set playeratk=6
set playerhp=25
)
if %playermorevaluedstat%==2 (
set playeratk=4
set playerhp=30
)

Batch Game - Draw a reference to a name

How do you ask the user for their name, not using %USERNAME% or something, but just asking for their input, then drawing a reference to that later.
This is my core so far
echo Who are you?
set /p Name = Enter your name:
set name= %Name%
echo Hello %name%
pause
goto WakeUp
:WakeUp
echo Who are you?
set /p Name=Enter your name :
echo Hello %name%
pause
:WakeUp

How to detect invalid user input in a Batch File?

I want to use a batch file to ask for a password to continue, i have very simple code that works.
#echo off
:Begin
cls
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
I need to detect when the user entered an invalid password, i have spent an hour researching but i found nothing. I'm not advanced in any way so try and keep it very simple like the code above.
Please help me.
You were missing exactly one line of code!
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
cls
Echo %pass% is not the PASSWORD
goto :Begin
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
If the password is invalid the code will simply continue to the next line which now is goto :Begin. This will restart the sequence. I changed the order around a bit so that the screen was cleared as well and the error was printed.
Similar to Monacraft's answer, but using if-else statements:
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password (goto Start) else *echo Invalid password. && goto :Begin)
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
What this does is it checks for the correct password. If it isn't the correct password, it will display an error message and go back to :Begin, which restarts the sequence.

How to use the echo command after a set /p command

I wanted to make this script work somehow, but i find that after you use
set /p variable= whater you want here
If you use the echo command after it, its hidden or something because it wont come up...
#echo off
cls
color A
:MAIN
cls
echo.
echo.
echo =================================
echo. set /p var= what is your name?
:: DOES NOT SHOW UP (STARTING HERE)
echo.
echo =================================
:: DOES NOT SHOW UP (ENDING HERE)
set /p answer= so your name is %var%?
IF %answer%== yes echo thats an amazing name!
IF %answer%== no goto MAIN
The part thats surrounded by the remarks doesnt want to show up for some reason... any ideas?
If I'm understanding what you are trying to do, this is the code you need:
#echo off
cls
color A
:MAIN
cls
echo =================================
echo what is your name?
echo =================================
set /p var=
set /p answer= so your name is %var%?
IF [%answer%] == [yes] echo thats an amazing name!
IF [%answer%] == [no] goto MAIN
pause
Because the processor will wait for the user input after printing what is your name? (due to the /p), you will not get the next line of ========etc. until enter is pressed.
It shows up for me, I get
=================================
set /p var= what is your name?
=================================
so your name is ? Andy
Which is what I would expect, what are you expecting?

Resources