I am starting to get into programming and recently learned the basics of batch script. I am doing something private, but whenever I try to use an input I put to "set/p", it exits the batch.
I'd like to know if there is something wrong with it
set /p msg=
if %msg%== y goto :y1
if %msg%== n goto c1
:y1
cls
color 0a
Before and after this there is just "echo" and text
Thanks in advance
You should use quotes around items when comparing, eg.:
if "%msg%" == "y"
Not at my computer now but four things to check / try.
Use IF /i so that it will work even if you type in CAPS. (http://ss64.com/nt/if.html )
As thebjorn said, its good to use the "" around the variable and the value.
Your example looks like it may be missing a space after the %msg%
The most likely issues based just on the code youve included is what Stephen said... In the example code, once it gets to the :y1 label would clear the screen, set color then do your echo super fast and exit.
Try the pause trick.
Thanks for the answer but I already figured it out.
I tried different changes and then I finally got it
I just needed to capitalize GOTO
#echo off
#echo [question] [message] y/n?
set /p msg=
if %msg%== y GOTO :y1
if %msg%== n GOTO c1
:y1
cls
color 0a
set /p msg=
if %msg%== y goto :y1
if %msg%== n goto :c1
:y1
cls
color 0a
I believe your error would be your beginning. Where you haveset /p msg=, this would have %msg% equaling nothing which if your batch program starts with an error then it will close immediately.
You can have it equal anything you want for example if you want it to be blank you would have to have: set /p msg=" " and in the Command Prompt it will just take your cursor to a blank line waiting for your input.
The most standard option is what command prompt uses to indicate a new line: set /p msg=" > "
I edited the code for you and you can change it as you wish... I cleaned up what it would look like a bit for you also
#echo off
echo input y or n
echo.
set /p "msg= > "
if "%msg%"=="y" goto :y1
if "%msg%"=="n" goto :c1
:y1
cls
color 0a
pause
Edit 1: fixed my quotes in the above code... the original placing wouldn't of done much with the above by itself but with additional code it may cause problems, when it comes to quotes and spaces batch is kinda picky
Side Note 1: if you want to have a timed pause instead of it waiting for a button press when it reaches pause, you can use the command timeout /t # the # would be the number of seconds you want it to wait and you can also press any button to have it be like the pause button... now just like how the pause button has the "Press any key to continue..." phrase so with timeout it has something like this but do not quote me on this "Press any key to continue or wait #..." you can hide the phrases from appearing but using either of these code lines: pause >nul or timeout /t # >nul
Related
I have a batch file that lets the user type in something then takes what they typed in and puts it as a value, let's say the value is called %input%.
I want the batch file to check for certain words in the %input% value and then use the goto command.
Anyone got any solutions?
Thanks.
EDIT
Here is some code that I am using:
#echo off
title Test
:Loop
Set /p Input=""
if "%input%"=="word *" goto function
I just need help figuring out how to check for "word" in the %input% value.
Hopefully this clears some things up.
Give a try for this code :
#echo off
:MainLoop
Color 0B
Cls
set /p "Input=Type something here "
echo %Input%|findstr /i "\<hello\>">nul && goto HelloFunction || goto NoHelloFunction
:HelloFunction
cls
Color 0A
echo I am into The Hello Function
pause
Goto MainLoop
:NoHelloFunction
cls
Color 0C
echo I am not into the Hello Function
pause
Goto MainLoop
Explanation
\< and \> means "Word boundaries", this avoids false positives (Helloo, Chello,...)
>nul redirects the found string to nul to keep the screen clear.
&& executes the set command only, if previous command (findstr) was successful.
|| means else and goto to another function
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
i am new here so i'll try to be as good as i can.
So i am trying to make a RPG based on text-based MS-DOS, and i am going pretty well as i just saw that if the user puts an invalid input at set /p, like an empty answer (just pressing enter) or an answer which is not on the "IF", the batch just crashes, and I would like to fix that so it will be less crashy.
Here is one of the parts i'd like to fix:
#echo off
title "Wasteland Adventure"
color 0A
cls
:Menu
cls
echo.
echo.
echo Welcome to Wasteland Adventure
echo.
echo To start a new game, type NEW and press ENTER.
echo To see instructions for the game, type INSTRUCTIONS and press ENTER.
echo To quit, type QUIT and press ENTER.
set input=
set /p input=What do you want to do?
if %input%==new goto INTRO
if %input%==instructions goto INSTRUCTIONS
if %input%==quit goto EXIT
Thanks in advance
it's not the set /pthat crashes, but:
if %input%==new
if %input% is empty, this is parsed as:
if ==new
obviously a syntax error. To avoid this, use:
if "%input%"=="new"
An empty input will then be parsed as:
if ""=="new"
which works fine.
The same applies when the variable contains only spaces and/or tabs:
if == new (syntax error) versus if " " == "new" (running fine)
Complete code like this:
:Menu
set input=
set /p input=What do you want to do?
if "%input%"=="new" goto INTRO
if "%input%"=="instructions" goto INSTRUCTIONS
if "%input%"=="quit" goto EXIT
REM for any other (invalid) input:
goto :Menu
Now i know how to create what i call tags
for example :startgame
And i know goto :startgame or in some occasions just goto startgame will take you back however
when i am doing if %selector% == r goto :Start_1
it just simply will close the batch file and i have tried with caps and without and without the :.
Now don't just be rude and call me ignorant i know basic batch
This is my start code
:Start_1
echo **************************************************
echo ************App selector by michaelukz************
echo **************************************************
echo **************************************************
echo To select an app press any button.
pause
This is the code i am trying to get to work
:Selected
echo You have selected your file.
echo If you wish to choose another file press R.
echo If you wish to close a program / file press C.
echo If you wish to close press any button.
SET /P"selected=Input letter here: "
if %selector% == r goto :Start_1
if %selector% == R goto :Start_1
if %selector% == c start taskmgr.exe
if %selector% == C start taskmgr.exe
if not timeout /T 3
echo Going to close menu
goto Closemenu
all the rest works except goto start_1.
Please help but don't be ignorrant - i have seen other people on here acting snarky as such.
A few things...
What you are calling tags are usually referred to as labels.
In your code, you are setting a variable called selected in this line SET /P"selected=Input letter here: ", but in your IF statements you are comparing to a variable called selector.
The line if not timeout /T 3 will not work. As #Magoo pointed out in a comment to the question, the syntax of if requires a comparison operator if [not] something compare-op something_else dothis.
take a look at CHOICE command, also you misspelled selector var name in SET /P
#echo off
:Start_1
echo **************************************************
echo ************App selector by michaelukz************
echo **************************************************
echo **************************************************
echo To select an app press any button.
:Selected
echo You have selected your file.
echo If you wish to choose another file press R.
echo If you wish to close a program / file press C.
echo If you wish to close press Q.
choice /C RCQ /N /M "Choose wisely [R,C,Q]" /D Q /T 30
goto action%errorlevel%
:action1
echo option R
goto start_1
goto selected
:action2
echo option C
start taskmgr.exe
goto selected
:action3
echo option Q
goto closemenu
goto Closemenu
I have a few things set up in a batch game. Instead of going where it is supposed to when the sure enters an option and hits "Enter" it goes to the next thing that starts with a : (I don't know what it is called).
Instead of it going to "Youtube" when the user types "Y".
:visitoption
echo Would you like to visit the RST Garry's mod gaming community website?
set /p option=Y or N:
if %option%==Y start chrome (Censored link)
if %option%==N cls goto :youtube
if %option%==y start chrome (Censored link)
if %option%==n cls goto :youtube
:version
cls
#echo off
echo.
echo[
#echo off
echo.
echo[
echo --Version--
echo Lightup Demo
#echo off
echo.
echo[
#echo off
echo.
echo[
#echo off
echo.
echo[
pause
goto :versionwhite
:youtube
echo Would you like to visit the Creator's Youtube channel?
echo Gameplay commentarys and such.
set /p option=Y or N:
if %option%==Y start chrome (Censored link)
if %option%==N goto :Beginning
if %option%==y start chrome (Censored link)
if %option%==n goto :Beginning
Essentially you were missing a command separator after the CLS but I've made some other changes such as /i case insensitive comparing and made the checking routines more robust to spaces or no input.
:visitoption
echo Would you like to visit the RST Garry's mod gaming community website?
set /p option=Y or N:
if /i "%option%"=="Y" start "" chrome "(Censored link)"
if /i "%option%"=="N" cls & goto :youtube
goto :visitoption
The "thing" is called a label
Since you have no control over what the user types, you should use
if "%option%"=="Y" start chrome (Censored link)
that is, quote both sides of the comparison (this is not bullet-proof, but serves adequately where the user is not deliberately trying to break your system.)
Adding the /i switch to the if will make the comparison case-insensitive.
if defined option set "option=%option:~0,1%"
will set option to just the first character.
Note that if the user replies simply Enter then the value of the variable remains unchanged. You can use this characteristic to your advantage
set "option=defaultvalue"
set /p option=Y or N:
will set option to defaultvalue if the user replies simply Enter.
start will start a process independently. The batch simply carries on to the next statement. You are probably beter off using start "window title for this instance" ... - it's a quirk of start that the first "quoted parameter" is used as the window title where you may be expecting it to be used as a parameter.
To concatenate a series of commands in a single line, you need to separate the individual commands with an ampersand &
Once you've turned echo off once, you don't need to do it again (unless you execute echo on, which you can do during debugging to show the program flow.) The leading # means don't echo this command - without it, the initial ECHO OFF would be reproduced.
You can use call :label to execute a subroutine that starts at :label in this batch file. If you use call label then the "subroutine" executed is the executable label. This is a very important distinction.
For this reason, I eschew the use of goto :label - although it works - because the colon is not necessary and for congruence between the goto and call commands.
The one exception to this omit-the-colons approach is where the colon actually does have an effect - goto :eof very specifically means 'goto the physical end of this batch file' - the label :eof is understood by cmd to have that meaning, and should not be defined in the batch.
You've set it so that it goes to youtube when the user presses n, not when the user enters y like you said your trying to in the question:
if %option%==N cls goto :youtube
Please try seeing what the cod eyour using is doing before you post a question on it.
And SO has one of the worlds easiest CodeSlabing systems. HOW BLOODY HARD IS IT TO PRESS SPACE 4 TIMES OR HIGHLIGHT AND PRESS THE "Code Sample" BUTTON?
Mona.