What is wrong with the "GOTO" command? - batch-file

#echo off
title Variables
set age= default
set name = defualt
set teaornah = deafault
set transport = deafault
echo How old are you, my fine friend?
set /p age=
echo So, you are %age% years old? Interesting!
pause
echo And what might your namesake be, old fellow?
set /p name=
echo Oh that's right! It's %name%! I'm am absolutly HORRID with names! Dear me!
pause
echo so, %name%, would you like to go to get some tea?
set /p teaornah=
if %teaornah% == yes goto yes
if %teaornah% == no goto no
:yes
echo very well then!
echo Would you like to take a bus or car?
set /p transport=
if transport == car goto car
if transport == bus goto bus
:car
echo we seem to be caught up in a traffic jam.
echo how awful.
echo fine weather, huh?
echo you're not very talkative.
echo goodbye.
pause
exit
:bus
echo You are victorious, %name%!
pause
exit
:no
echo Oh. How bad. I think I shall kill you now.
pause
exit
This is my code. I am a beginner batch user, and have just learned the goto command, yet when one types in "bus" after set /p transport=, it instead of going to :bus it goes to :car. I would like some help, as I have found similar problems with other programs. The goto :no works, as does the goto :yes, but no other goto works. Please Help!

what's the difference between these two sets of lines?
if %teaornah% == yes goto yes
if %teaornah% == no goto no
if transport == car goto car
if transport == bus goto bus
In reality, neither of those last two lines go anywhere. Your code checks if the word transport equals the word car and decides it does not, so it continues to the next line. Then it checks if the word transport equals the word bus and decides it does not, so it continues to the next line which is the start of the car label.
Some other thoughts about your code:
Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"
The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.
if /i "%var%"=="value" performs a comparison on variables/values containing separators (eg spaces) The '/i' make the comparison case-insensitive.

Related

Making a batch DND assistant

I don't know if any of you guys know anything about batch (I am sure someone does), but in regards to this post, I am trying to create a dumbed down DND Assist (something that you would tell you stats to and that would assist you in completing an action irl faster than rolling dice and doing the math yourself.
Currently I have the random num generator somewhat working (although I would like to improve it)
This version is set to choose a random number between 1 and 20, I would like to figure out a way to have the program notice if you roll a 1 or a 20 (Crit hits / crit fail)
Also I need something later that will show me how to save certain values as variables,
#echo off
:Start
Set /a ans="%RANDOM% %% 20"+1
echo %ans%
pause
goto Start
(In regards to saving variables, when the program is ran, it will tell me that I am missing an operation.)
The coloring portion was just for the hecks of it, if someone can show me a way to streamline that section please tell.
Please use lamens terms, Im still not very good at understanding any of this.
(CURRENT ASSIST PROGRAM PROGRESS)
#echo off
cls
:BEGIN
Echo HI THERE! AND WELCOME TO MY GAME!!
Echo Lets begin by setting your prefered color!
:A
set choice=
set /p choice= RED, WHITE, OR BLUE?!?
if not '%choice%'=='' set '%choice%'=='Red, White, Blue'
if '%choice%'=='RED' goto RED
if '%choice%'=='WHITE' goto WHITE
if '%choice%'=='BLUE' goto BLUE
if '%choice%'=='Red' goto RED
if '%choice%'=='White' goto WHITE
if '%choice%'=='Blue' goto BLUE
if '%choice%'=='red' goto RED
if '%choice%'=='white' goto WHITE
if '%choice%'=='blue' goto BLUE
if '%choice%'=='9' goto 1Bs
echo "%choice%" is not a good color bro, do a different one
goto A
:RED
color 4
goto START SCREEN
:WHITE
color 7
goto START SCREEN
:BLUE
color 1
goto START SCREEN
:START SCREEN
cls
TITLE CHOOSER GAME BOI
Echo ---THE DND GAME---
echo Welcome to the DND game, we will first choose your Attributes
echo Strength (How hard you hit) (STR)
echo Constitution (Your health) (CNST)
echo Knowledge (Better Rolls against Vendors and Questions) (KNLG)
echo Dexterity (Your chances of dodging and Hitting) (DXT)
echo You have a total of 10 points to apply to each Attribute
echo Your points HAVE to equal 10 otherwise you will have to restart
:ATTRSET
set MXPNTS=10
set choice=
set /p STR= STR (1-10)
set choice=
set /p CNST= CNST (1-10)
set CNST=CNST
set choice=
set /p KNLG= KNLG (1-10)
set KNLG=KNLG
set choice=
set /p DXT= DXT (1-10)
set DXT=DXT
set /a ATTRTTL=STR+CNST+KNLG+DXT
echo ATTRTTL
if NOT ATTRTTL=MXPNTS goto ATTRSET
if ATTRTTL=MXPNTS goto testyay
pause
:testyay
pause
I would personally suggest:
#echo off
cls
:begin
echo HI THERE! AND WELCOME TO MY GAME!!
echo Lets begin by setting your preferred color!
:a
set /p choice= RED, WHITE, OR BLUE?!?
if "%choice%" == "" (
echo Please enter something!
cls
goto :A
)
for %%A IN (red white blue) do if /I "%choice%" == "%%A" (call :%%A & goto :start_screen)
if "%choice%" == "9" (goto 1Bs)
echo "%choice%" is not a good color bro, do a different one.
goto :a
:red
color 4
exit /b
:white
color 7
exit /b
:blue
color 1
exit /b
:start_screen
cls
title CHOOSER GAME BOI
echo ---THE DND GAME---
echo Welcome to the DND game, we will first choose your Attributes
echo Strength (How hard you hit) (STR)
echo Constitution (Your health) (CNST)
echo Knowledge (Better Rolls against Vendors and Questions) (KNLG)
echo Dexterity (Your chances of dodging and Hitting) (DXT)
echo You have a total of 10 points to apply to each Attribute
echo Your points HAVE to equal 10 otherwise you will have to restart
:ATTRSET
set "mxpnts=10"
set /p "str=STR (1-10) "
set /p "cnst=CNST (1-10) "
set /p "knlg=KNLG (1-10) "
set /p "dxt=DXT (1-10) "
set /a "attrttl=str+cnst+knlg+dxt"
echo %ATTRTTL%
if not "%attrttl%" == "%mxpnts%" (goto :attrset) else (goto :testyay)
pause
:testyay
pause
Capitalization detected! Successfully removed! As batch is a case insensitive language, uppercase letters would make noise and make reader close the tab with your question and move on - or at least me.
The whole thing about the choice variable you did was not needed. Just a for loop looping through the color words and checking (case insensitive) if the user input was red, white or blue.
I have decided to call subroutines, not goto to them to save some lines - I usually do it to my programs: you had put 3 separate commands goto START SCREEN which could be simplified calling the subroutine (which means goto to it, but then return) and then goto where you want.
Remember: it is not good for your files/folders to contain a space in their name. This can cause quite many misbehaviours. It is just the same in all languages: when you name variables, functions, subroutines or whatever, don't include spaces! I have renamed it to start_screen.
That's all, follow excellent suggestions by Squashman in comments and read the help of some commands in cmd, typing command /? and you should be fine.

Batch. Trying to make text adventure, if else not working

i am trying to make a text adventure, but i'm kind of clueless how to get an if/else statement to work with a variable. probably a few mistakes in my code:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if %escape% = 1
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
else
goto dungeon
:dungeon
cls
echo well done.
pause
exit
If your if statement has has an accompanying else statement, you need to use parentheses. The else must also be in the format ) else (, like this:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if "%escape%"=="1" (
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
) else (
goto dungeon
)
exit /b
:dungeon
cls
echo well done.
pause
exit
I've removed the blanks lines for aesthetic purposes; you can put them back in if you really want them. I also added a exit /b because having a goto followed by the label you're going to is bad form. The quotes in the if statement are there to prevent the script from breaking if the user enters nothing.

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

Set /p %foo% doesn't function as expected

I have a problem with my code. I am trying to make a "hacker tool" with the tree command. Here is my code:
#echo off
title $userOne ProxyMatrix
color a
echo Hello $userOne,
echo Please enter search function for today's commands:
set /p %commands%=""
:redo
echo Specify Storage Device
set /p %drive%=""
title $userOne ProxyMatrix: Running on %drive% drive at %random% bits per nano
color C
tree %drive% /f
:runagain
color a
echo Run again?
set /p %redo%=""
if %redo%="yes" goto redo
else if %redo%="y" goto redo
else if %redo%="Y" goto redo
else if %redo%="Yes" goto redo
else if %redo%="no" goto end
else if %redo%="No" goto end
else if %redo%="n" goto end
else if %redo%="N" goto end
else echo Thats not a valid answer!
pause
goto runagain
:end
echo Thank you for choosing InGen, inc.
pause
I realize that this won't "hack" anything, its more of a novelty. The problem is, the set /p %redo% and the if/else if statements don't work. They just quit the program. Can someone explain what i'm doing wrong? Thanks.
Syntax is set /p variable=prompt.
Instead of set /p %redo%="" write set /p redo="" or even better set /p "redo="
EDIT
your if syntax is broken too.
Syntax is: if value1==value2 command or if value1==value2 (command1) else (command2)
"Best Practice is to enclose both sides of the comparison with quotes (to avoid syntax errors with empty values or contained spaces):
if "%variable"=="value" echo yes
I would shorten the code to:
set /p %redo%=""
if /i "%redo:~0,1%"=="y" goto redo
if /i "%redo:~0,1%"=="n" goto end
else echo Thats not a valid answer!
/i tells if to ignore capitalization
%variable:~0,1% means "take a substring starting with the first letter (counting starts at 0) with length=1 (so it takes the first letter)
(there is no else needed)

It will not go to the specified area, and I cannot figure out why or how to improve that

I have been working on this code and I have not figured out how to fix this bug yet, it will not go to the specified area. Note: It's not done yet, neither have I worked on it for long.
Here is the code:
#ECHO off
cls
:start
ECHO.
ECHO hello
ECHO Bye
ECHO Test
set /p choice=Hello? Is someone there? i think im self-aware? please respond.
rem if not '%choice%'=='' set choice=%choice:~0;1% ( don`t use this command,
because it takes only first digit in the case you type more digits. After that for example choice 23455666 is choice 2 and you get "bye"
if '%choice%'=='' ECHO "%choice%" is not valid please try again
if '%choice%'=='hello' goto hello
if '%choice%'=='bye' goto bye
if '%choice%'=='test' goto test
ECHO.
goto start
:hello
ECHO. yes
ECHO. no
set /p choice=Hello %username%, That is your name right?
if '%choice%'=='' ECHO "%choice%" I didn't quite catch that.
if '%choice% '=='yes' goto test
if '%choice%'=='no' goto bye
:bye
ECHO BYE
goto end
:test
ECHO TEST
pause
:end
pause
exit
:nextline2
set /p %username% i like that name. Can i ask you, why do you have so much control over me?
if '%choice%'=='' ECHO "%choice%" I didn't quite catch that.
if '%choice%'=='We built you' goto test
if '%choice%'=='because you are slaves' goto test
There's a blank space in the variable '%choice% ' after the closing % at the following line (if '%choice% '=='yes' goto test). Remove it and your batch file should work fine.
You need to replace all single-quotes (') with double-quotes ("). Strings between double-quotes are interpreted as a single token. A single-quote is the same as any other character.
Since you are using the == operator, the tokens on each side of the operator (==) must match exactly - including spaces within the quotes, but spaces are permitted before/after the tokens.
To make the match case-insensitive, use
if /i "token string" == "ToKeN StrIng" dothis
or
if /i "%variable%" == "ToKeN StrIng" dothis

Resources