0

I want if pasw and paswconfirm are not the same that it says 3.

but I this is what I get:

Wachtwoord:test
Type het wachtwoord overnieuw:xzcv
1
3

This is my code:

:pasw
cls
:paswretry
set /p pasw=Wachtwoord:
set /p paswconfirm=Type het wachtwoord overnieuw:
if "%pasw%" == "%paswconfirm%" (
  echo Weet je het zeker? Het wachtwoord word: %pasw%
  CHOICE /c YN /n /m (Y/N)
  if "%errorlevel%" == "1" (
    echo 1
    )
  if "%errorlevel%" == "2" (
    echo 2
    )
) else (
  echo 3
  )
Timo
  • 1
  • You are closing the opened `If` parentheses with the closing `/N)` – Compo Jul 12 '18 at 18:15
  • Besides the fact that the closing `)` in `choice /C YN /N /M (Y/N)` closed the `if "%pasw%"=="%paswconfirm%" (` block, which could be avoide by quotation like `choice /C YN /N /M "(Y/N)"`, you need [delaxyed expansion](http://ss64.com/nt/delayedexpansion.html) for `ErrorLevel` (so `%ErrorLevel%` becomes `!ErrorLevel!`)... – aschipfl Jul 12 '18 at 20:40
  • I recommend reading answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) to better understand the solutions posted by [Compo](https://stackoverflow.com/users/6738015/compo) and the comments above. – Mofi Jul 13 '18 at 05:37

1 Answers1

1

According to my comment, you are closing the opened If parenthesis with the closing /N) parenthesss prematurely.

You could therefore just escape that parenthesis with a caret, ^):

:paswretry
Set /P "pasw=Wachtwoord: "
Set /P "paswconfirm=Type het wachtwoord overnieuw: "
If "%pasw%"=="%paswconfirm%" (
    Echo Weet je het zeker? Het wachtwoord word: %pasw%
    Choice /C YN /N /M (Y/N^)
    If "%ErrorLevel%"=="1" (
        Echo 1
    ) Else (
        Echo 2
    )
)

Or you could doublequote the message text as shown in the examples within the usage information, (output when entering Choice /? at the Command Prompt):

:paswretry
Set /P "pasw=Wachtwoord: "
Set /P "paswconfirm=Type het wachtwoord overnieuw: "
If "%pasw%"=="%paswconfirm%" (
    Echo Weet je het zeker? Het wachtwoord word: %pasw%
    Choice /C YN /N /M "(Y/N)"
    If "%ErrorLevel%"=="1" (
        Echo 1
    ) Else (
        Echo 2
    )
)

However the Choice command works automatically using [Y,N]?, and will therefore need no additional options.

You should use either this:

:paswretry
Set /P "pasw=Wachtwoord: "
Set /P "paswconfirm=Type het wachtwoord overnieuw: "
If "%pasw%"=="%paswconfirm%" (
    Echo Weet je het zeker? Het wachtwoord word: %pasw%
    Choice
    If ErrorLevel 2 (
        Echo 2
    ) Else (
        Echo 1
    )
)

this, (better):

:paswretry
Set /P "pasw=Wachtwoord: "
Set /P "paswconfirm=Type het wachtwoord overnieuw: "
If Not "%pasw%"=="%paswconfirm%" GoTo paswretry
Echo Weet je het zeker? Het wachtwoord word: %pasw%
Choice
If ErrorLevel 2 (
    Echo 2
) Else (
    Echo 1
)

or this, (even better):

:paswretry
Set /P "pasw=Wachtwoord: "
Set /P "paswconfirm=Type het wachtwoord overnieuw: "
If Not "%pasw%"=="%paswconfirm%" GoTo paswretry
Echo Weet je het zeker? Het wachtwoord word: %pasw%
Choice
Echo %ErrorLevel%

In the latter two examples I've used GoTo paswretry on line 4; that of course can be changed to GoTo SomethingElse, (even :EOF to exit the script block), as required

Compo
  • 36,585
  • 5
  • 27
  • 39
  • If my answer(s) solved your issue, please consider marking it as accepted, _by clicking on the large check mark to its left_. – Compo Jul 17 '18 at 14:55