Reputation: 962
I could not find a solution to this problem, and stackoverflow is the only place I thought I could find a solution. I was creating a batch program to generate a random password. I wanted it to have a Random number of Characters and random letters. Here is the script I used to generate it-
set /a rchar=%RANDOM%*8/32768+16
:chargen
set /a charnum=%RANDOM%*36/32768+1
if %charnum%==1 set char='a'
if %charnum%==2 set char='b'
if %charnum%==3 set char='c'
if %charnum%==4 set char='d'
if %charnum%==5 set char='e'
if %charnum%==6 set char='f'
if %charnum%==7 set char='g'
if %charnum%==8 set char='h'
if %charnum%==9 set char='i'
if %charnum%==10 set char='j'
if %charnum%==11 set char='k'
if %charnum%==12 set char='l'
if %charnum%==13 set char='m'
if %charnum%==14 set char='n'
if %charnum%==15 set char='o'
if %charnum%==16 set char='p'
if %charnum%==17 set char='q'
if %charnum%==18 set char='r'
if %charnum%==19 set char='s'
if %charnum%==20 set char='t'
if %charnum%==21 set char='u'
if %charnum%==22 set char='v'
if %charnum%==23 set char='w'
if %charnum%==24 set char='x'
if %charnum%==25 set char='y'
if %charnum%==26 set char='z'
if %charnum%==27 set char='0'
if %charnum%==28 set char='1'
if %charnum%==29 set char='2'
if %charnum%==30 set char='3'
if %charnum%==31 set char='4'
if %charnum%==32 set char='5'
if %charnum%==33 set char='6'
if %charnum%==34 set char='7'
if %charnum%==35 set char='8'
if %charnum%==36 set char='9'
set %char%=%ipassword%
set %fpassword%=%ipassword%%fpassword%
set /a rchar=%rchar%-1
if %rchar%==0 goto :passworddone
goto :chargen
I kept getting "The syntax of this error is incorrect" when trying to run it. Does anyone have any ideas for a solution? It would be very much appreciated! Thank you!
Upvotes: 0
Views: 111
Reputation: 881113
First things first, don't use %
markers on the left of a set
statement. Something like:
set fpassword=xyzzy
set %fpassword%=plugh
will set the xyzzy
variable to be plugh
, a form of indirection.
It should be:
set fpassword=plugh
Second, there's actually a rather concise (though tricky) method of doing substrings with variable offsets and lengths in cmd
(something that normally only allows constants) so there's no need for all those if
statements.
The trick is to call set
as a sub-function. The following script:
@echo off
set string=abcdefgh
set ofs=2
set len=3
rem Here be dragons :-)
call set result=%%string:~%ofs%,%len%%%
echo %result%
shows this in action and will output:
cde
Keep in mind that this is a zero-offset method so you don't want to add 1
to the variable when selecting a random value to use as the index.
And lastly, your variable manipulations seem a little off, as you're (for example) trying to set char
again regardless of the random value chosen, and also doing something torturous with ipassword
and fpassword
.
Taking all that into account, the script can be better written as something like:
@setlocal
@echo off
set /a count = %RANDOM% * 36 / 32768 + 1
set charlist=abcdefghijklmnopqrstuvwxyz0123456789
set password=
:while1
set /a index = %RANDOM% * 36 / 32768
call set char=%%charlist:~%index%,1%%
set password=%password%%char%
set /a count = %count% - 1
if %count% gtr 0 goto :while1
echo %password%
endlocal
The other thing you may want to consider is the password length. One-character passwords are a very bad idea and I'm not sure anyone is going to remember a 36-character password. It may be better to have a password length of (for example) 8 through 12 inclusive:
set /a count = %RANDOM% %% 5 + 8
(and, yes, that %%
is the modulo operator - you can use that instead of the multiply/divide actions).
Upvotes: 2