Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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  If %INPUT%=="n" goto no If %INPUT%=="Y" goto yes If %INPUT%=="N" goto no :yes java -jar lib/RSBot-4030.jar -dev echo Starting RSbot in developer mode :no java -jar lib/RSBot-4030.jar echo Starting RSbot in regular mode pause 

Either way if the user enters y or n it always runs in -dev mode.

How do I make it run in -dev mode if the answer is yes, and regular mode if the answer is no. Also, how do I make it ask again if the input isn't Y, N, y, or n?

like image 611
Jonathan Beaudoin Avatar asked Aug 18 '12 18:08

Jonathan Beaudoin


People also ask

How do you automate input in command prompt?

To avoid this manual input, use the command "echo Y | del /P " in your batch script to answer the prompt. You can try this echo command to pass input (ex: answer for username and password prompts) to your console application, when it is invoked through batch script.

How do I parameterize a batch file?

You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter's fully qualified path name). The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file's drive and path).

How do I ask a question in a batch file?

To have a batch file stop and prompt for input, you would want to look into using set /p , which will prompt for input and then assign that input to a variable. e.g.: set /P name="What Is Your Name? " echo Hello %name%!


1 Answers

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.

like image 86
Andriy M Avatar answered Oct 01 '22 20:10

Andriy M