Useful Tips

CMD Windows Command Line Selection Operators


Are you really good at programming batch files, just don’t know how to make a menu with a choice of yes, no or choice 1, 2 or 3? You have come to the right place!

  1. 1 Click Start> Run
  2. 2 Enter "cmd" (without quotes)
  3. 3 Enter "edit"
  4. 4 Enter the following commands. After each press enter. All that is in parentheses is NOT required, these are notes with explanations.
  5. 5 @echo off (This command "hides" the ability to enter commands - optional, but we recommend using it)
  6. 6 cls (Hides everything above is optional, but if you want everything to look orderly, we recommend it)
  7. 7: start
  8. 8 echo.
  9. 9 echo Choice 1 ("Choice 1" can be renamed as you need)
  10. 10 echo Choice 2
  11. 11 echo Choice 3 (Insert as many choices as you need).
  12. 12 Enter "set / p choice = (Here, insert a question or command, for example," Yes or no?")
  13. 13 if not "% choice%" == set choice =% choice:


  • 14 if "% choice%" == "1" goto: choice1
  • 15 if "% choice%" == "2" goto: choice2
  • 16 (Continue following this example until you reach the number of options you need. Then enter :)
  • 17 echo "% choice%" is not a valid option. Please try again.
  • 18 echo.
  • 19 goto start
  • 20 Then enter:
  • 21: choice1
  • 22 (commands to execute)
  • 23 goto end
  • 24: choice2
  • 25 (teams)
  • 26 goto end
  • 27: choice3
  • 28 (teams)
  • 29 goto end
  • 30 Continue until you enter the number of commands you need.
  • 31: end
  • 32 pause
  • 33 exit
  • 34 Save as a file with a .bat extension. To check the batch file, double-click on it.
  • New user

    AT Bash there are several commands that can allow you to create a script in which you can describe several actions and provide the user with the number of the desired position and then certain commands are executed, if the user has not selected anything, the default number is substituted and the commands corresponding to this number are executed.

    How can this be done at the Windows command prompt.

    Please provide examples or articles with a detailed analysis of the features of such teams in BATCH (without using any other languages ​​or installing utilities)


    Very kind Developer

    CHOICE [/ C list] [/ N] [/ CS] [/ T timeout / D element] [/ M text]

    This program allows you to select an item from a list of items
    and returns the index of the selected item.

    / C list Creates a list of selectable items.
    The default list is "YN".

    / N Hide the list of items in the prompt.
    A message is displayed before the invitation
    and the items remain available for selection.

    / CS Enables case sensitivity when selecting items.
    By default, the program is not case sensitive.

    / T timeout The number of seconds before selecting the default item.
    Valid values ​​are from 0 to 9999.
    If 0 is specified, there is no pause before selection
    item by default.

    / D element The element selected by default in nnnn seconds.
    The symbol must be present in the element set.
    in / C, nnnn is specified in / T.

    / M text Message displayed before
    an invitation. If not specified, the program
    only prompts.

    /? Display help for use.

    ERRORLEVEL environment variable gets index value
    the key corresponding to the selected item. First element
    in the list returns 1, the second - 2, etc.
    If the user presses a key that is not appropriate
    selectable, a beep sounds. If an error is detected,
    returns ERRORLEVEL 255. If keys are pressed
    CTRL + BREAK or CTRL + C, the program returns an ERRORLEVEL value of 0.
    When using the ERRORLEVEL parameters in a batch program, their
    should be listed in descending order.

    CHOICE /?
    CHOICE / C YNC / M "Y - Yes, N - No, C - Cancel."
    CHOICE / T 10 / C ync / CS / D y
    CHOICE / C ab / M “a” to select parameter 1 and “b” to select parameter 2.
    CHOICE / C ab / N / M “a” to select parameter 1 and “b” to select parameter 2.

    After 5 seconds, Y will be automatically selected.
    The serial number of the selected letter will be saved as an error return code.

    Note that capitalization (if / CS is not specified) is ignored in the English alphabet only.