0

I'm trying to make sure the user of the batchfile is inputing a 6 digit numbers only. No more no less and only numbers.

I found the code bellow in this thread but can't figure how to modify it for my need.

Reference code


:Prompt
SET /P "UserInput=Please Enter Version as X.X.X.X: "
FOR /F "TOKENS=1-4 DELIMS=." %%i IN ("%UserInput%") DO (
    SET /A n1=1*%%i
    SET /A n2=1*%%j
    SET /A n3=1*%%k
    SET /A n4=1*%%l
)
IF NOT "%UserInput%" == "%n1%.%n2%.%n3%.%n4%" GOTO :Prompt
ECHO %UserInput%

I thought that modifying the code like so would do the trick but it does not

My modification to the reference code


:Prompt
SET /P "UserInput=Please Enter Version as XXXXXX: "
FOR /F "TOKENS=1-6" %%i IN ("%UserInput%") DO (
    SET /A n1=1*%%i
    SET /A n2=1*%%j
    SET /A n3=1*%%k
    SET /A n4=1*%%l
    SET /A n4=1*%%m
    SET /A n4=1*%%n
)
IF NOT "%UserInput%" == "%n1%%n2%%n3%%n4%%n5%%n6%" GOTO :Prompt
ECHO %UserInput%

What i'm I doing wrong and i'm I on the right track? Thank you and have a nice day!

  • 3
    See my answer on [Safe number comparison in Windows batch file](https://stackoverflow.com/a/57111885/3074564). In addition to what I wrote in referenced answer the length can be checked with `if not "%UserInput:~6,1%" == "" echo You input more than six digits.& goto Prompt` and `if "%UserInput:~5,1%" == "" echo You input less than six digits.& goto Prompt`. – Mofi Sep 08 '20 at 17:27
  • There is no need for the `FOR` command if you removed the periods from the input. – Squashman Sep 08 '20 at 18:27
  • The reference code and your modified one are different situations, the former uses a delimiter character between the numbers (`.`), the latter not, so `for /F` will not help. You could do this: `:INPUT`, then `set "UserInput=" & set /P UserInput="…"`, then `cmd /V /C echo(!UserInput!| findstr "^[0-9][0-9][0-9][0-9][0-9][0-9]$" || goto :INPUT` – aschipfl Sep 08 '20 at 18:41
  • @Mofi If you want to, you could post your comment as an answer, this is what I used and it works now. Thank you very much. – cinqrougedesign Sep 08 '20 at 19:48

2 Answers2

1

You could avoid labels or verfication by simply restricting the input to allowed characters and length, then just append any 'fixed' characters to the string.

@Echo off &Setlocal EnableDelayedExpansion
Set nVar=
Echo/Enter 6 Digit Integer:&(For /L %%# in (1 1 6)Do For /F "Delims=" %%G in ('Choice /N /C 0123456789')Do (<Nul Set /P"=%%G"&Set "nvar=!nVar!%%G"))&Echo/
Echo/Var [!nVar!] Entered
Endlocal

If you need numerical strings of varying lengths, then you can turn the above into a macro and use substring modification to supply the number of digits required.

@Echo off & Setlocal EnableDelayedExpansion
Set "Input=(Set "nVar="&Echo/Enter # Digit Integer:&(For /L %%. in (1 1 #)Do For /F "Delims=" %%G in ('Choice /N /C 0123456789')Do (<Nul Set /P"=%%G"&Set "nvar=^^!nVar^^!%%G"))&Echo/)"
%Input:#=6% & Echo/Var [!nVar!] Entered
Endlocal
T3RR0R
  • 1,830
  • 2
  • 7
  • 17
1

You could do the following:

:INPUT
rem // Clear variable to not use previous value if user just presses {Enter}:
set "UserInput="
rem // Prompt for user input:
set /P UserInput="Please enter version as XXXXXX: "
rem // Evaluate user input:
cmd /V /C echo(!UserInput!| > nul findstr "^[0-9][0-9][0-9][0-9][0-9][0-9]$" || goto :INPUT
echo Entered version is %UserInput%.

To avoid a few other characters, like 2 and 3 (available on German keyboards), or also 1, depending on the current code page, to be unintentionally accepted, change each expression [0-9] to [0123456789].

aschipfl
  • 28,946
  • 10
  • 45
  • 77
  • 1
    Just a small addition: The expression `[0-9]` includes also `¹` with hexadecimal code value B9 in [Windows-1252](https://en.wikipedia.org/wiki/Windows-1252) and Unicode and hexadecimal code value FB in [code page 850](https://en.wikipedia.org/wiki/Code_page_850) (Western European code page for Windows console). This character is not included in [code page 437](https://en.wikipedia.org/wiki/Code_page_437) (North American code page for Windows console). – Mofi Sep 09 '20 at 06:03