0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Do you start it with a "doubleclick"? If yes, the window will disappear as soon as the batchfile ends. Use `pause` at the end of your script to keep it open and read the output. – Stephan Jan 02 '15 at 08:48
  • Remove the spaces after each `==`. Correct syntax is `if "string"=="string" do stuff`. See `help if` in a console window for more info. – rojo Jan 03 '15 at 05:38
  • 1
    @rojo the syntax presented in `help if` is a stylistic issue, not a semantic issue, iow. spaces around `==` are fine. You would need to enclose `%msg%` in quotes to prevent issues if the user didn't enter anything. If you put quotes around the lhs., you'll also need to put quotes around the rhs. (i.e. `"y"`). – thebjorn Jan 03 '15 at 12:34

4 Answers4

0

You should use quotes around items when comparing, eg.:

if "%msg%" == "y"
thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • Whitespace in batch scripts is important. Remove the spaces on either side of `==`. – rojo Jan 03 '15 at 05:36
  • @rojo while it is true that white space is important in many places in batch scripts, it is definitely not necessary to remove the white space around `==` (try it!). – thebjorn Jan 03 '15 at 12:25
  • for an explanation of this issue, see here: http://stackoverflow.com/a/27489844/2152082 – Stephan Jan 04 '15 at 09:29
0

Not at my computer now but four things to check / try.

  1. Use IF /i so that it will work even if you type in CAPS. (http://ss64.com/nt/if.html )

  2. As thebjorn said, its good to use the "" around the variable and the value.

  3. Your example looks like it may be missing a space after the %msg%

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

Durry42
  • 401
  • 3
  • 10
0
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

MackuliN
  • 1
  • 4
  • Whitespace in batch scripts is important. Remove the spaces after each `==`. – rojo Jan 03 '15 at 05:36
  • @rojo yes you are correct that error is my bad. Thank you for point that out i have now fixed it. – MackuliN Jan 03 '15 at 23:25
  • @Stephan yes it does but every way i have ever seen it or have used it, it has always been `set /p "var"=" "` or since there is a use of just a space you can write it like `set /p "var= "`... you are correct `set /p var= works fine but you have to think of how picky batch can be sometimes, something else could be added later and mess it all up. – MackuliN Jan 06 '15 at 02:53
  • @MackuliN syntax is: `set /p variable=` can be anything including being empty; `set /p "var=input: "` just looks nicer on the screen than without the space. – Stephan Jan 06 '15 at 08:58
0

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
  • 1
    uhm - batch commands are not case sensitive. Just capitalizing 'goto' definitively doesn't help. You certainly had a different error. – Stephan Jan 05 '15 at 14:04