Inane Coding Go on, slap that wayward code


Simple user input in batch files

I was recently playing around with batch scripts (*.bat files) - but what I wanted to do with the script snowballed and I found I needed more functionality, the main one being to be able to execute commands based on user input.

Below is an example of the code I used to do this. This is for Windows NT based batch execution (i.e. any recent computer).

SET /P _inputname= Please enter an input:
IF "%_inputname%"=="OMG" GOTO :they_said_omg
ECHO No 'OMG'... frowny face :(
GOTO :end
ECHO You said OMG!

The first line of code is performing two actions:

  • Displaying the text "Please enter an input:" to the screen;
  • Waiting for the user response, and then setting an environment variable (in this case, named '_inputname') to whatever string the user enters.

This is accomplished by using the SET command, which in this case has the following syntax:

SET /P variable=[promptString]


The '/P' switch tells the command interpreter to prompt the user for an input (which is saved into the variable). The promptString is the text that is displayed to the user.

The end result of this line is that you are asked to provide an input, and whatever input this is, it gets stored into an environment variable which can be used later in the code.

The second line of code then uses an 'IF' statement, together with a 'GOTO' statement, to test this saved environment variable and decide what code to execute next. Note the unusual syntax for this IF statement;

IF "%_inputname%"=="OMG" GOTO :they_said_omg


The IF statement tests if one thing is equal to another, but because the first item is an environment variable (the one previously entered by the user), it must have the percentage '%' signs used either side of the variable name, as well as the quote marks. There is also a double-equals sign which in batch scripting is used to test equality of A==B (rather than a single equals sign which is used for assigning A=B).

In this example, this IF statement tests if the text that the user entered was "OMG" - if it was, then the IF statement is true, and so the GOTO statement is then executed. This makes the code execution 'go-to' the line which has the matching label of ":they_said_omg". If the text that the user entered was not "OMG", then the GOTO command is not executed and the code carries on as normal.

When all put together this means that if the user enters the text "OMG", the following command is executed:

ECHO You said OMG!

and if they enter anything else, then this is executed:

ECHO No 'OMG'... frowny face :(

Hope this helps!

Comments (7) Trackbacks (0)
  1. What would I put after the 2 equal signs if I wanted it to be blank? For example

    echo What is your name?
    set /p name= Type your name
    if %name%==BLANK goto …….

    BLANK would be if they don’t type anything, but I know you don’t type BLANK so what do you put there instead?

    • I think it would just be “” , instead of “OMG”. The quote character ” is used to denote the start and end of the string (word), so if the end comes straight after the start “” then this means its empty (blank). They would still have to press enter though.

    • Not sure if it is still helpful but you would go like

      set /p inputname=Text||set inputname=nothing
      if “%inputname%”==”Nothing” goto :end

  2. Wow, this is very helpful for a newbie like me.
    Thanks a lot,

  3. Thanks! Most comprehensive article on the subject iv see!

  4. I have a question can you make a sentence like:

    set /p input=
    if %input%==hi how are you goto end
    does it work or must it be 1 word?

    • Hi Peter, yes that would work as long as you get the syntax correct. You need quotes around the text you are matching with, and a colon before the label to go to, so you would use (not tested);

      IF “%input%”==”hi how are you” GOTO :end

Leave a comment

CAPTCHA human-ness test: *

No trackbacks yet.