Here is the batch code rewritten and commented to accept any combination of 1
, 2
, 3
or 4
as well as any invalid input.
@echo off
rem Enable delayed expansion as the user as the freedom to enter any string
rem like an empty string or a string containing 1 or more double quotes or
rem command line operators which could result in undefined behavior or exit
rem of batch execution because of a syntax error on evaluation without using
rem delayed expansion. Please note that not escaped exclamation marks are
rem not interpreted anymore as literal characters, but as begin or end of
rem a delayed expanded environment variable reference.
setlocal EnableExtensions EnableDelayedExpansion
:UserPrompt
cls
set "ValidChoice=0"
set "pcws="
set /P "pcws=Please select 1-4: "
rem Was anything entered by the user at all?
if not defined pcws goto UserPrompt
rem The 4 IF conditions below compare if the user input string with all
rem 1, 2, 3 or 4 replaced by nothing is not equal with the unmodified
rem user input string which means it checks if the user input string
rem contains one or more times 1, 2, 3 or 4.
:UserChoice
echo/
if not "!pcws:1=!" == "!pcws!" set "ValidChoice=1" & goto Choice1
if not "!pcws:2=!" == "!pcws!" set "ValidChoice=2" & goto Choice2
if not "!pcws:3=!" == "!pcws!" set "ValidChoice=3" & goto Choice3
if not "!pcws:4=!" == "!pcws!" set "ValidChoice=4" & goto Choice4
rem Has the user entered at least one of the requested characters?
if %ValidChoice% == 0 goto UserPrompt
endlocal
goto :EOF
:OneMoreChoice
echo/
pause
rem Remove all 1, 2, 3 or 4 from user input string.
set "pcws=!pcws:%ValidChoice%=!"
rem Is the environment variable still defined which means the
rem user input string contains perhaps more choices to process?
if defined pcws goto UserChoice
endlocal
goto :EOF
:Choice1
echo Wrong choice^^!
goto OneMoreChoice
:Choice2
echo Thanks.
goto OneMoreChoice
:Choice3
echo Please try again.
goto OneMoreChoice
:Choice4
echo Have a nice day.
goto OneMoreChoice
On batch file execution the user can enter
- nothing ... prompt is displayed again;
"<>|
... prompt is displayed again;
4321
... all 4 messages are output and batch file processing ends;
1>nul
... first message is output and batch file processing ends;
2,3
... second and third message is output and batch file processing ends;
2-4
... second and fourth message is output and batch file processing ends.
It is also possible to insert above the comment lines above label UserChoice
following command lines:
rem Accept only a user input containing the digits 1-4 without any other
rem character between or with space(s) or comma(s) anywhere in string.
for /F "delims=,1234 " %%I in ("!pcws!") do goto UserPrompt
The additional FOR command line results in a jump to label UserPrompt
if the input string contains any other character than ,1234
or a space character. The test inputs 1>nul
and 2-4
would not be accepted anymore with the additional FOR command line. Only 4321
, 2,3
and for example 1,3 4, 2
would be still interpreted as valid input string being further processed.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
See also Single line with multiple commands using Windows batch file for an explanation of operator &
used to run two commands on one command line to avoid usage of command blocks.
And read also DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ why it is better to use echo/
instead of echo.
to output an empty line.