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

前端 未结 9 1913
温柔的废话
温柔的废话 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:49

    Here is a working example:

    @echo off
    :ask
    @echo 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
    goto ask
    :yes
    @echo you select yes
    goto exit
    :no
    @echo you select no
    goto exit
    :exit
    @pause
    
    0 讨论(0)
  • 2020-12-23 14:50

    I have improved batch file with yes or no prompt. If user enter any character except y and n , then it will again prompt user for valid input. It Works for me.

        @echo off
    
        :ConfirmBox 
            set /P c= Are you sure want to contine (y/n)?
    
        if /I "%c%" EQU "Y" (
        goto :FnYes 
        ) else if /I "%c%" EQU "N" ( 
        goto :FnNo
        ) else ( 
        goto :InValid 
        )
    
    
    :FnYes
         echo You have entered Y
         goto :END
    
    :FnNo
         echo You have entered N
         goto :END
    
    :InValid
         echo Invalid selection. Enter Y or N
         goto :ConfirmBox
    
    :END
        pause
        exit  
    

    /I in if condition will validate both lowercase and uppercase characters.

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

    i just do :

    set /p input= yes or no
    if %input%==yes echo you clicked yes
    if %input%==no echo you clicked no
    pause
    
    0 讨论(0)
  • 2020-12-23 14:53

    I don't know the platform you're doing this on but I assume Windows due to the .bat extension.

    Also I don't have a way to check this but this seems like the batch processor skips the If lines due to some errors and then executes the one with -dev.

    You could try this by chaning the two jump targets (:yes and :no) along with the code. If then the line without -dev is executed you know your If lines are erroneous.

    If so, please check if == is really the right way to do a comparison in .bat files.

    Also, judging from the way bash does this stuff, %foo=="y" might evaluate to true only if %foo includes the quotes. So maybe "%foo"=="y" is the way to go.

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

    Add quotation marks (" ") around the %INPUT% so it looks like this:

    If "%INPUT%" == "y" goto yes
    If "%INPUT%" == "n" goto no
    If "%INPUT%" == "Y" goto yes
    If "%INPUT%" == "N" goto no
    
    0 讨论(0)
  • 2020-12-23 14:54

    try this for comparision

    if "%INPUT%"=="y"...
    
    0 讨论(0)
提交回复
热议问题