0

I wanted to make a batch file that automatically generates passwords. Here is my code:

@Echo Off
:a
color 0b
Setlocal EnableDelayedExpansion
Set _RNDLength=35
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo !_RndAlphaNum!
goto a

I do not get what I was looking for as result when I execute it.

There is output:

1LKoZsi4DEMY8dwQ3xnFGHp1lKEcGsiMIpO
Iz6KsfPIeEqR1lMrx
E3LnXBkXYtpU
vUnDFwMM
UzBao
zYACtvBB
7cWXu
qOKu
ft
hA
lhB
ECHO is off.
KW
jJ
7a
8I
kN
iAy
Ex
ty
ObS
ECHO is off.
1
2
5Z
D
T9x
ECHO is off.
1e
6
ECHO is off.
O
Maximum setlocal recursion level reached.
S
Maximum setlocal recursion level reached.
f
Maximum setlocal recursion level reached.
m
Maximum setlocal recursion level reached.
eQ
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
ls
Maximum setlocal recursion level reached.
q
Maximum setlocal recursion level reached.
Lw
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
1M
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
L
Maximum setlocal recursion level reached.
p
Maximum setlocal recursion level reached.
TR
Maximum setlocal recursion level reached.
sR
Maximum setlocal recursion level reached.
J
Maximum setlocal recursion level reached.
ECHO is off.
Maximum setlocal recursion level reached.
0j
Maximum setlocal recursion level reached.

What should I do?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • You don't specify "what you're looking for," but you can at least stop the `Maximum setlocal recursion level reached.` errors by moving the `setlocal enabledelayedexpansion` line up three lines to be just above `:a`. – SomethingDark May 01 '22 at 03:51
  • This is precisely the same code as your prior post https://stackoverflow.com/q/72073923/2128947, except that you've changed the required length to 35 and moved the label `:a` to a place other than where I said it should go. You now have `setlocal` in a loop and haven't ensured that your variables are cleared before generating your passwords. In fact, you could clear the problem you have with ***THIS*** code by simply inserting an `endlocal` line before the `goto a` – Magoo May 01 '22 at 17:34

1 Answers1

1

I'm having a very hard time understanding what your could is supposed to do. So I'm just going to write some code that is likely similar to your goal.

This code creates 5 passwords, 35 characters long each. Changing this simply requires modifying DigitCount and LoopCount to desired values.

@ECHO OFF
    SetLocal EnableExtensions EnableDelayedExpansion
    SET Digits=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
    SET DigitCount=35
    SET LoopCount=5
:PWStart
    SET "PW=_"
:LoopDigit
    SET /A RndIdx=%RANDOM% * 62 / 32768
    SET "PW=%PW%!Digits:~%RndIdx%,1!
    IF "!PW:~%DigitCount%,1!" == "" GOTO :LoopDigit
    ECHO %PW:~1%
    SET /A LoopCount-=1
    IF %LoopCount% GTR 0 GOTO :PWStart
GOTO :EOF

EDIT: Thanks @Mofi, updated code to suggestions.

It looks like @Mofi's suggestions are no longer visable, but I believe they have considerable value to those learning to program in the Windows batch files.

  1. I failed to include EnableExtensions in the SetLocal command. This works for the scripts I write since extensions are already enabled by default. But when presenting code to the public, there are those who will test the code in an environment that does not have extensions enabled. It's not a big deal to make this change, so it will probably wise to change this habit for all of my Batch scripts.
  2. In the original code, I used EQU instead of ==, EQU is intended for use with numeric values, where == is intended for use with string values. @Mofi explains this about two thirds of the way down in this answer.
Darin
  • 1,423
  • 1
  • 10
  • 12