DevSupport
asked on
creating a random password in batch
I am trying to create a random password using batch.
I googled and found a method but it involves setlocal delayed exapansion and all which is affecting my existing code (with special characters and stuff)
This is the code I got:
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKL MNOPQRSTUV WXYZabcdef ghijklmnop qrstuvwxyz 0123456789
Set _Str=%_Alphanumeric%987654 321
:_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 !!_Alphanu meric:~%_R ND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo Random string is !_RndAlphaNum!
However I dont know how to store the result into a variable in say x so that I can get it as %x%
Like x=!_RndAlphaNum!
echo %x% can show me the random number generated.
unrelated question:
Is there a way to generate a random alpha-num string without using setlocal enabledelayedexpansion.
I googled and found a method but it involves setlocal delayed exapansion and all which is affecting my existing code (with special characters and stuff)
This is the code I got:
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKL
Set _Str=%_Alphanumeric%987654
:_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
If !_count! lss %_RNDLength% goto _loop
Echo Random string is !_RndAlphaNum!
However I dont know how to store the result into a variable in say x so that I can get it as %x%
Like x=!_RndAlphaNum!
echo %x% can show me the random number generated.
unrelated question:
Is there a way to generate a random alpha-num string without using setlocal enabledelayedexpansion.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SET X=!_RndAlphaNum!
Then use %X%, i.e.
ECHO %X%