How do I request and receive user input in a .bat and use it to run a certain program?

前端 未结 9 1914
温柔的废话
温柔的废话 2020-12-23 14:37

This is what I have so far

@echo off
:Ask
echo Would you like to use developer mode?(Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If %INPUT%==\"y\" goto yes          


        
相关标签:
9条回答
  • 2020-12-23 14:55

    If the input is, say, N, your IF lines evaluate like this:

    If N=="y" goto yes 
    If N=="n" goto no
    …
    

    That is, you are comparing N with "y", then "n" etc. including "N". You are never going to get a match unless the user somehow decides to input "N" or "y" (i.e. either of the four characters, but enclosed in double quotes).

    So you need either to remove " from around y, n, Y and N or put them around %INPUT% in your conditional statements. I would recommend the latter, because that way you would be escaping at least some of the characters that have special meaning in batch scripts (if the user managed to type them in). So, this is what you should get:

    If "%INPUT%"=="y" goto yes 
    If "%INPUT%"=="n" goto no
    If "%INPUT%"=="Y" goto yes
    If "%INPUT%"=="N" goto no
    

    By the way, you could reduce the number of conditions by applying the /I switch to the IF statement, like this:

    If /I "%INPUT%"=="y" goto yes 
    If /I "%INPUT%"=="n" goto no
    

    The /I switch makes the comparisons case-insensitive, and so you don't need separate checks for different-case strings.

    One other issue is that, after the development mode command is executed, there's no jumping over the other command, and so, if the user agrees to run Java in the development mode, he'll get it run both in the development mode and the non-development mode. So maybe you need to add something like this to your script:

    ...
    :yes
    java -jar lib/RSBot-4030.jar -dev
    echo Starting RSbot in developer mode
    goto cont
    :no
    java -jar lib/RSBot-4030.jar
    echo Starting RSbot in regular mode
    :cont
    pause
    

    Finally, to address the issue of processing incorrect input, you could simply add another (unconditional) goto command just after the conditional statements, just before the yes label, namely goto Ask, to return to the beginning of your script where the prompt is displayed and the input is requested, or you could also add another ECHO command before the jump, explaining that the input was incorrect, something like this:

    @echo off
    :Ask
    echo Would you like to use developer mode?(Y/N)
    set INPUT=
    set /P INPUT=Type input: %=%
    If /I "%INPUT%"=="y" goto yes 
    If /I "%INPUT%"=="n" goto no
    echo Incorrect input & goto Ask
    :yes
    ...
    

    Note: Some of the issues mentioned here have also been addressed by @xmjx in their answer, which I fully acknowledge.

    0 讨论(0)
  • 2020-12-23 15:06
    echo off
    setlocal
    SET AREYOUSURE = N
    :PROMPT
    set /P AREYOUSURE=Update Release Files (Y/N)?
    if /I %AREYOUSURE% NEQ Y GOTO END
    set /P AREYOUSURE=Are You Sure you want to Update Release Files (Y/N)?
    if /I %AREYOUSURE% NEQ Y GOTO END
    
    echo Copying New Files
    
    :END
    

    This is code I use regularly. I have noticed in the examples in this blog that quotes are used. If the test line is changed to use quotes the test is invalid.

    if /I %AREYOUSURE% NEQ "Y" GOTO END
    

    I have tested on XP, Vista, Win7 and Win8. All fail when quotes are used.

    0 讨论(0)
  • 2020-12-23 15:14

    Depending on the version of Windows you might find the use of the "Choice" option to be helpful. It is not supported in most if not all x64 versions as far as I can tell. A handy substitution called Choice.vbs along with examples of use can be found on SourceForge under the name Choice.zip

    0 讨论(0)
提交回复
热议问题