Looking at your code it appeared to me that using Call
would solve both your nested label and delayed expansion issues, and probably make your code easier for you to manage.
@Echo Off
Set "uname=mama"
Set "pword=mu"
Set "user="
Set "pass="
:Inputs
For /L %%G In (1,1,3) Do (
Call :xy
If Not ErrorLevel 1 GoTo Main
)
"%__AppDir__%choice.exe" /M "Would you like to try again"
If Not ErrorLevel 2 GoTo Inputs
Exit /B 1
:Main
Rem Your code below here for users with successful inputs.
Echo Welcome %user%.
Timeout /T 3 /NoBreak > NUL
Rem Your code ends above here.
Exit /B 0
:xy
ClS
Set /P "user=Enter Username> %user%"
If /I Not "%uname%" == "%user%" (
Echo Username not found.
Set "user="
Timeout /T 2 /NoBreak > NUL
Exit /B 1
)
Echo Username is valid.
Set /P "pass=Enter Pass> "
IF "%pword%" == "%pass%" Exit /B 0
Echo Invalid password.
Set "pass="
Timeout /T 2 /NoBreak > NUL
Exit /B 1
You may notice that I've used the recommended syntax for defining variables with the Set
command. It ensures that the content is protected and does not have things such as hidden trailing spaces. If you take a look at the code in your question, you will note that the value assigned to the uname
variable is actually mama<space>
. This would obviously cause issues when mama
is locked out of the rest of the script!
You also notice had an unwanted newline instead of a space between echo
and username is valid
.
Edit
The code above was re-worked to allow for up to three attempts at entering both values correctly, although the try again
message seems a little redundant then.