I've recently written a password script in batch. Basically it takes the password from a different batch script, finds out how many characters it has, takes the password from the user and compares them. However, nothing is ever simple with me and I've gotten into some nested FOR loops and such. It's hard to explain, so here's the code:
@echo off
setlocal enabledelayedexpansion
call pass.bat & rem Sets the variable "pass" as "default".
set map=abcdefghijklmnopqrstuvwxyz
set len=0
for /l %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!pass:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
for /l %%C in (1,1,100) do (
set letter%%C=!pass:~%%C,1!
)
:pass
for /l %%D in (0,1,%len%) do (
cls
choice /c "abcdefghijklmnopqrstuvwxyz" /n /m "Password: !ast!"
set /a charerr=!errorlevel!-1
for /l %%E in (0,1,25) do (
set password=!password!%map:~!charerr!,1% & rem This is the problem line.
)
set ast=!ast!*
)
if "%pass%" neq "%password%" goto fail
cls
echo Correct Password
echo pass: %pass%
echo password: %password%
>nul pause
exit
:fail
set /a tries-=1
if %tries% geq 1 goto pass
Now, the script doesn't crash or anything like that, however it does not set password as the password you entered.
If you run it you'll understand.
Note: pass.bat purely contains the line set pass=default