How could I limit the user input to only two numeric characters in the command below?
set "nfilial="
set /p "nfilial=Number Filial (2 Digits):"
for /f "delims=1234567890" %%a in ("%nfilial%") do goto :nfilial
How could I limit the user input to only two numeric characters in the command below?
set "nfilial="
set /p "nfilial=Number Filial (2 Digits):"
for /f "delims=1234567890" %%a in ("%nfilial%") do goto :nfilial
There are many different ways I can think of.
You could use substring extraction to ensure that the 2nd character is not null, and the third character is.
:nfilial
set /p "nfilial=Number Filial (2 Digits):"
for /f "delims=1234567890" %%a in ("%nfilial%") do goto nfilial
if "%nfilial:~1"=="" goto nfilial
if not "%nfilial:~2"=="" goto nfilial
Or if you prefer, you could zero pad the left side, then extract the last two characters regardless of the number entered.
:nfilial
set /p "nfilial=Number Filial (2 Digits):"
for /f "delims=1234567890" %%a in ("%nfilial%") do goto nfilial
set "nfilial=0%nfilial%"
set "nfilial=%nfilial:~-2%"
You could add a numeric check to that if you wish, to ensure that if %nfilial% lss 0 goto nfilial
and if %nfilial% gtr 99 goto nfilial
, but that's probably overkill.
Or you could force user entry of two numerals with the choice
command. Or you could use findstr
to match a regexp of "^[0-9][0-9]$"
. Or, because set /a
only calculates integers, you could try to divide by 100 and make sure the result is 0. Or there are probably other ways. Really, you're limited only by your imagination. You could turn it into a Rube Goldberg assembly of checks. You could have PowerShell evaluate it, then use conditional execution to goto nfilial
based on the exit code. You could compile a c# program. You could have the script email you the value entered and wait for you to respond with an OK. It just depends on how bored you are, really.
You should realize that one thing is to read any input from the user, check that the input is correct and repeat it if it does not, and an entirely different thing is to limit the user input to the required input format only. The first method may be achieved in several ways, but if you want to use the second method, you may do it via ReadFormattedLine subroutine (suggested in the link given above), that is written in pure Batch. Using it, you may solve your problem this way:
call :ReadFormattedLine nfilial="##" /M "Number Filial (2 Digits):"
You may download ReadFormattedLine subroutine from this post.