-1

I tried to run password generator from youtube. Here's the code:

@echo off
chcp 1257
setlocal EnableDelayedExpansion
set alpha= 
aąbcčdeęėfghiįjklmnopqrsštuųūvwxyzžAĄBCČDEĘĖFGHIĮJKLMNOPQRSŠTUŲŪVWXYZŽ

For /L %%j in (1,1,13) DO CALL:GEN
echo Your Random Password is [ %PASSWORD% ]

EndLocal
pause

:GEN

For /L %%j in (1,1,10) DO (
if %random% gtr 10000 ( set PASSWORD=%PASSWORD%%random:~0,1% ) else (
set /a i=%random:~1,1%+%random:~1,1%
if !i! gtr 25 set i=25
set PASSWORD=%PASSWORD%!alpha:~%i%,1! )
)

This returns 1 random password. And I need 10 passwords, so I tried to create for loop before this line: For /L %%j in (1,1,13) DO CALL:GEN . Then it returns 10 rows of text but without passwords:

Active code page: 1257
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Your Random Password is [  ]
Press any key to continue . . .

How can I solve this?

Lukas
  • 11
  • 1
  • 2
  • there is no way, your shown script producing the shown output. Please correct. – Stephan Apr 20 '18 at 21:17
  • You can't use `%i%` like that. You would need to use delayed expansion with that variable. Which still will not work because you are inside a code block and you can't do a substring like that. – Squashman Apr 20 '18 at 21:34
  • Why reinvent the wheel. Just borrow code when you can. http://jasonfaulkner.com/RandomChars.aspx – Squashman Apr 20 '18 at 21:57
  • Because I need this for university and jasonfaulkner way is too complicated.. :D Why I'm not doing this by myself? Because I'm not planning to use batch someday and I don't like it. So I'm trying to fix this code. I tried to create my own password generator but it was too dificult for me – Lukas Apr 20 '18 at 22:01

2 Answers2

3
@echo off & setlocal EnableDelayedExpansion
chcp 1257

set "alpha=aabccdeeefghiijklmnopqrsštuuuvwxyzžAABCCDEEEFGHIIJKLMNOPQRSŠTUUUVWXYZŽ"
set alphaCnt=70

For /L %%j in (1,1,10) DO CALL :GEN %%j

pause
Goto :Eof
:GEN
Set "Password="
For /L %%j in (1,1,10) DO (
    Set /a i=!random! %% alphaCnt
    Call Set PASSWORD=!PASSWORD!%%alpha:~!i!,1%%
)
echo Your Random Password %1 is [%PASSWORD%]

Sample output:

Your Random Password 1 is [EÜllxleUOc]
Your Random Password 2 is [RBGEoÄulEF]
Your Random Password 3 is [AfuuAEFwMe]
Your Random Password 4 is [kuaEjuLicr]
Your Random Password 5 is [ModgGsANÄE]
Your Random Password 6 is [MzEqSWJWCB]
Your Random Password 7 is [oÜcrFUqGpj]
Your Random Password 8 is [kRHDCqiciÜ]
Your Random Password 9 is [gUYjjSiicQ]
Your Random Password 10 is [cuuAOÜixVY]
  • Thank you!. Also, maybe you can explain what ~!i!,1%% means? I didn't find any info in the internet – Lukas Apr 21 '18 at 13:14
  • The var I gets the result of the modulus division (integer remainder) of `!random! %% alphaCnt` assigned - a random pointer into the string alpha. To append the char pointed to by `!I!`you need delayed expansion twice because inside a (code block), so the pseudo call with doubled percent signs is used. –  Apr 21 '18 at 16:06
1

Try this instead. you can add/remove characters to the alp variable.

@echo off
setlocal enabledelayedexpansion
set "alp=@ # ^& % a A b B c C d D E e f F g G h H I I j J k K l L m M n N p P q Q r R s S t T u U v V w W x X y Y z Z 0 1 2 3 4 5 6 7 8 9"
set "cnt=0"
for %%a in (%alp%) do (
    set "rn.!cnt!=%%a"
    set /a "cnt+=1"
)
for /l %%i in (1,1,10) do (
 set "pssw="
 for /l %%a in (1,1,13) do (
    set /a "rand=!random! %% cnt"
    for %%b in (!rand!) do set "pssw=!pssw!!rn.%%b!"
)
 echo Your random password is: [ !pssw! ]
)

note, you cannot use ! as a character, due to the fact that we're using delayedexpansion here.

Gerhard
  • 22,678
  • 7
  • 27
  • 43