Set /p %foo% doesn't function as expected - batch-file

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)

Related

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

What could be the reason for error message Unexpected Goto in batch file with many string compares?

Here is the code of my batch file:
:add_game_now
cls
echo Add a Game!
echo V for Victory and D for Defeat
set /p add_game=
if %add_game%==V goto add_game_now_v
if %add_game%==D goto add_game_now_d
:add_game_now_v
set /a elo=%elo%+20
if %division%==bronze goto add_game_bronze_v
if %division%==silver goto add_game_silver_v
if %division%==gold goto add_game_gold_v
if %division%==platinum goto add_game_platinum_v
if %division%==diamond goto add_game_diamond_v
if %division%==master goto add_game_master_v
if %division%==challanger goto add_game_challanger_v
:add_game_now_d
set /a elo=%elo%-15
if %division%==bronze goto add_game_bronze_d
if %division%==silver goto add_game_silver_d
if %division%==gold goto add_game_gold_d
if %division%==platinum goto add_game_platinum_d
if %division%==diamond goto add_game_diamond_d
if %division%==master goto add_game_master_d
if %division%==challanger goto add_game_challanger_d
The problem is that when I am at :add_game_now and enter V or D I get the message Unexpected Goto and then it exits.
What could be the reason for this error message?
If you need the entire file I can send it.
Thanks for helping and Yes I am not good at coding.
Instead of enumerating all these GOTO target labels you can use the value of the variable within the label name:
SETLOCAL EnableExtensions
REM goto :EOF will terminate the batchfile
REM this only works if Extensions to CMD are enabled
:add_game_now
cls
echo Add a Game!
echo V for Victory and D for Defeat
set /p add_game=
GOTO add_game_now_%add_game%
GOTO :EOF
:add_game_now_V
:add_game_now_v
set /a elo=%elo%+20
GOTO add_game_%division%_v
GOTO :EOF
:add_game_now_D
:add_game_now_d
set /a elo=%elo%-15
GOTO add_game_%division%_d
GOTO :EOF
Make sure that all these labels exist! Otherwise the batch will crash. The variable division also has to exist and must have a value (like 'bronze').
Consider to use CHOICE instead of set /p. This way, you can make sure that only uppercase entries are allowed and only 'V' or 'D'. You will not need the doubled labels anymore then.

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

What is wrong with the "GOTO" command?

#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.

How to find keywords of the users input in the command line?

What I'm looking for is a batch file line of code that will scan what the user inputs to find key words and direct them in the right place. That way when a trainee has a question he/she could just ask the batch file and it will direct them to the proper menu. Is this possible? if so, How would one go about doing this?
:menu
set /p c=Please type you question:
findstr /m "How to ringout a product on our system?" "%c%"
if %c%=="ringout" (
goto :Ringingout
) Else (
goto :Sorry
)
:Ringingout
cls
echo In order to right something out maksure you do the following:
echo - Log in
echo - click on scan in the bottom left had corner on the tender page
echo - scan items
echo - click continue
Pause
goto :Menu
:Sorry
cls
echo Sorry I don't recognize your question, please re-word it.
pause
goto :Menu
#ECHO OFF
SETLOCAL
SET "keywords=word anotherword someword"
FOR %%k IN (%keywords%) DO set "#%%k="
SET /p query="Please enter your query ? "
FOR %%k IN (%keywords%) DO CALL :analyse %%k
SET #
GOTO :EOF
:analyse
CALL SET "found=%%query:%1=%%"
IF "%found%"=="%query%" GOTO :EOF
SET #%1=FOUND!
GOTO :eof
Here's a general way to do it.
If one of the keywords is entered, the variable #keyword will be set to FOUND! so you can use if defined #keyword to process from there.
It's not protected against destructive user-inputs - that's not what this question is about...
You'd be better off collecting all the questions in a document (like .html) and letting the user search that document for what they need. But if this is just an exercise, you can re-write your logic like so to make your program work:
:menu
set "c="
set /p "c=Please type your question: "
echo %c% | findstr /i /b "ringout" >nul
if errorlevel 1 goto Sorry else goto Ringingout
:Ringingout
and so on.

Resources