Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello! I know there is a simple way to do this. I do not know how. So can someone help me with this question:
When i have a /p variable and the user enters the wrong variable how can i stop it from closing
Lets say i have:
set /p a=Choose a option:
And the two options are start and quit.
they say
Choose a option: quitt
Oh no! Command prompt closes!
I cannot find a solution can anyone help?
________________________________________________________________________
Remember "help"!
Posted

1 solution

The code could be something like this:
@echo off
set /p var=Choose a option ('s' to start, 'q' to quit):
if %var%==q goto:eof
echo working!


In this sample, if you enter 'q', "working!" will not be printed, because you go to the end of the script. You also can use functions with batch files (!). Please see my recent answer:
Batch variable equations[^].

Chances are, your code already works correctly; but you probably you haven't started command interpreted, so it was not closed, but the console of you batch was closed. If you started it with CMD.EXE and then started your batch from the command line. Or, alternatively, start it with "CMD /K your_script_batch". Again, please see the help created by the script I referenced in my previous answer.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900