0

I want to create a batch file that prompts a user to enter a number sequence that is 6 numbers long. I also want to check if that input includes, for example, three of the number 5 within it.

@echo off
set /p UserInput=
if %UserInput% has........(
goto Number_Has_Three_5
)else(
goto Number_Dosent_Have_Three_5
)

This would check if the input has only numbers and then if it has three of the number five in it.

For example, the number 111155 is six numbers long but doesn't have three fives so it wouldn't work and it would goto the error message.

But the number 111555, since it has three of the number five in it instead of two fives, would work and the script would continue.

Compo
  • 36,585
  • 5
  • 27
  • 39
maximuslotr
  • 74
  • 1
  • 1
  • 9

2 Answers2

3

This is a matter of doing 4 different tasks:

There are two ambiguities to your question however:
Do the three 5s need to be together (152535 = false, 123555 = true)?
Can there be more than three 5s (125555 = false, 555123 = true)?

My code allows for the 5s to be anywhere in the 6 character string, and will output true if there are at least exactly three of them.

:: ...
if not defined UserInput (
    echo False. No Input.
    goto:eof
)

set "testVar="
for /f "delims=0123456789" %%i in ("%UserInput%") do set testVar=%%i
if defined testVar (
    echo False. Not Numeric.
    goto:eof
)

set testVar=%UserInput%000000
if not %testVar:~0,6%==%UserInput% (
    echo False. Not 6 digits.
    goto:eof
)

set testVar=%UserInput:5=%
if not defined testVar goto:true
if not %testVar:~3,1%#==# (
    echo False. Less than three 5s.
    goto:eof
)
:: Edit: More than three 5s result in false
if %testVar:~2,1%#==# (
    echo False. More than three 5s.
    goto:eof
)

:true
echo True.
:: ...
BDM
  • 3,760
  • 3
  • 19
  • 27
  • Very good code! I want to suggest one more `if` condition block below the first one with first line being `set "UserInput=%UserImput:"=%"` to remove all by mistake entered double quotes. Then once again `if not defined UserInput (` is used to make sure environment variable `UserInput` is still defined at all after removing all `"`. The reason is that on German keyboard pressing by mistake __Shift+2__ instead of just __2__ results in typing `"` instead of `2`. A user input string with one or more `"` can break execution of batch file because of a syntax error. – Mofi Jun 09 '19 at 16:20
  • @Mofi Great suggestion. Would definitely recommend anyone relying on user input sterilise the resulting string. I think it's a bit out of scope for my answer as it wasn't in the original question. There's also a lot more that could break the script than just a `"`, and I don't want to account for them all. – BDM Jun 10 '19 at 01:12
1
@echo off
:loop 
set /p "userinput=Input: "
echo %userinput%|findstr /r "^[0-9][0-9][0-9][0-9][0-9][0-9]$" >nul || (
    echo invalid input
    goto :loop
)
set "nofives=%userinput:5=%0123456"
set /a fives=%nofives:~6,1%
echo %userinput% has %fives% fives.
if %fives% == 3 goto :three
goto :loop
:three
echo insert your payload here

findstr checks, if the input consists of exactly 6 numbers.

set nofives... removes any 5 and adds a counter string

%nofives:~6,1% gets the seventh character (counting is zero-based!) which is the count of fives.

As this effectively counts the number of fives (expandable to an input of max nine numbers 1)), you are free to handle "exactly three" (if %fives% == 3) or "minimum three" (if %fives% geq 3) or whatever you want.

To show how it works, let's assume %userinput% is abcdef (just to not confuse it with the counter string) If we remove nothing from %userinput% and add the counter string, it looks like:

abcdef0123456
      ^ this char is the count (seventh char)

If we remove one char (abcdef -> abcdf) and add the counter string, it looks like:

abcdf0123456
      ^ this char is the count (seventh char)

and so on until "remove six chars (for you that would be an user input of 555555). then it looks like:

0123456
      ^ this char is the count (seventh char)

1) with two slight changes expandable up to 15 numbers (using a hexadecimal counter string):

set "nofives=%userinput:5=%0123456789ABCDEF"
set /a fives=0x%nofives:~6,1%
Stephan
  • 53,940
  • 10
  • 58
  • 91