Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

batch file - Random alphanumeric

I am trying to make a random code generator in Notepad++ that will generate a alphanumeric code that is 32 characters long and does not include the letters "O or E". Below is a code I tried to modify to fit my needs but I cannot seem to get it to work. I am a total noob here and really have little idea of what I am doing. Also the it is a sort of decoding tool that will be operated in a CMD window.

Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=abcdfghijklmnpqrstuvwxyz0123456789
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 Random string is !_RndAlphaNum!

Any help would be great and thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
@echo off

    setlocal enableextensions enabledelayedexpansion

    rem Define alphabet
    set "alphabet=a b c d f g h i j k l m n p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"

    rem Create an "array" with the elements of the alphabet
    set "size=0"
    for %%a in (%alphabet%) do (
        set "a.!size!=%%a"
        set /a "size+=1"
    )

    rem Generate the output, selecting 32 randoms elements from the array
    set "k="
    for /l %%a in (1 1 32) do (
        set /a "r=!random! %% size"
        for %%b in (!r!) do set "k=!k!!a.%%b!"
    )
    echo(%k%

    endlocal

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...