1

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

1 Answer 1

2

You could try

call set "password=!password!%%map:~!charerr!,1%%"

Your variant can't work, as percent expansions are expanded when a block is parsed, so the %map:~!charerr!,1% will be expanded, but fails as !charerr! isn't expanded at that time.

The CALL can start a second parse time when evaluating the line and the double percent will then expand correct.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your solution, however I also needed to take out the nested for loop because it wasn't necessary int he first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.