1.

Solve : Batch: Input typed incorrectly?

Answer»

Hi, I am creating a small batch file that turns the user's input into a COMMAND.
For EXAMPLE,
Code: [Select]set /P input=What would you like to do?
if /I "%input%" EQU "EXIT" exit
if the user types "exit" then the cmd window will close.

My problem is that if the user inputs the command incorrectly (e.g. "eixt" instead of "exit") or inputs something that the cmd does not understand, then the cmd window closes on its own or goes to the NEXT label (e.g. :new).
How would you set it so that if the user inputs something UNKNOWN or incorrect then the cmd displays something like "The word "whateverwastypedwrong" is not a command."? I would like to know how to do it without using:
Code: [Select]if /I "%input%" NEQ "EXIT" echo "The word "whateverwastypedwrong" is not a command."
because if there are many commands such as "EXIT", "NEW", "COPY", "MOVE", etc etc etc then that single command line can become a bit of a mess.

Is there more of an efficient way of solving this issue?Why do you feel the need to do this? The fact that you foresee the error-checking getting unwieldy is a clue that it might be a bad idea. Anyhow, cmd.exe has done it all for you. It's called the prompt. One way I might do it (if I had to!) might be to have a text file containing a list of all the commands I wanted to select from and use FOR to compare the user input with each entry in turn.

Basically I am creating a file/folder management program with a batch file that can work on any of my associates computers so that they can manage notes taken in lectures efficiently AND because I'm simply interested in this small project anyway
That idea sounds interesting. Are there any alternatives to using a text file with a command list in it such as a line or a few lines of code that achieve the same thing without having to CREATE a text file and checking the commands in it? If not, then I'll probably go with that idea.When I first read your initial query I though you meant that you wanted users to be able to type in native console commands such as DIR, etc, but now I see (I think) that you wish them to be able to input command words of your own devising. You can verify that typed input matches an item in a list without using a separate file; the list can be held in a comma or space-delimited string...

Code: [Select]@echo off
set commandlist=start stop run copy move open close pause resume erase insert exit
echo Available commands: %commandlist%
:loop
set /p UserInput="Your command? "
set verified=0
for %%A in (%commandlist%) do if /i "%%A"=="%UserInput%" set verified=1
if "%verified%"=="0" (
echo Incorrect input - please try again
goto loop
)
echo Command OK

Ah yes! Thats exactly what I was looking for. Thank you very much
I'll mark this as solved.



Discussion

No Comment Found