Advertisement
T3RRYT3RR0R

All Purpose Random String Generator Batch

Mar 9th, 2021 (edited)
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 12.96 KB | None | 0 0
  1. @echo off
  2.  
  3. (Set \n=^^^
  4.  
  5. %= \n macro newline variable. Do not modify =%)
  6.  
  7. ::#---------------------------------------------------------------------------------------------------
  8. ::#| $rStr Args:         Description:
  9. ::#|
  10. ::#|  /L Int            : Length of each Substring [MANDATORY] - At least one length must be supplied.
  11. ::#|  /L Int Int Int    : Varying Lengths may be provided for each Substring
  12. ::#|                      by supplying the length for the corresponding iteration.
  13. ::#|                      /R Iterations is defined as the number of lengths provided.
  14. ::#|   /Mod Int IntSum  : Supply a Range for a Random Length at provided Index.
  15. ::#|                       - Example: Two part string Part 1 = 5 characters, Part 2 = 7-15 characters:
  16. ::#|                          /L 5 .  /Mod :0 9+7 /D :
  17. ::#|                         - OVERIDES /L 5 . TO /L 5 [RandomInt 7-15]
  18. ::#|                       * Modifier value should be at same index of /Mod SubArgs as '.' is in /L:SubArgs
  19. ::#|                         - Use 0 values for Modifier list to retain /L lengths at original index.
  20. ::#|
  21. ::#|  /Rep Int          : Number of Iterations to repeat pattern as an Integer
  22. ::#|                      - Use /Rep when all Iterations of pattern are to be same /L length
  23. ::#|
  24. ::#|  /V ReturnVar      : Define Supplied returnvar with generated String
  25. ::#|                          - If no return var is supplied, value will be stored in $rStr_RV
  26. ::#|
  27. ::#|  /A ReturnVar      : Append generated string to supplied return variable.
  28. ::#|
  29. ::#|  /P Pattern Subargs:
  30. ::#|     - Predefined   : ANM - English Letters in Upper and Lower case + 0~9 [ DEFAULT ]
  31. ::#|                    : ANL - English Letters in Lower case + 0~9
  32. ::#|                    : ANU - English Letters in Upper case + 0~9
  33. ::#|                    : AL  - English Letters in Lower case
  34. ::#|                    : AU  - English Letters in Upper case
  35. ::#|                    : N   - 0~9
  36. ::#|                    : H   - Hex String: ABCDEF0123456789
  37. ::#|
  38. ::#|     - Range        : Character Range  - Provide a range to use as the Character Set.
  39. ::#|                       - Example:     /P [3-w] = 3456789abcdefghijklmnopqrstuvw
  40. ::#|                       - Range Order: 0-9a-zA-Z ; Total Available Range: 0-Z
  41. ::#|
  42. ::#|     - Custom       : "Character String"
  43. ::#|                       - provide a custom string of characters to use. Example:
  44. ::#|                         /L 3 3  /P "-+@$^&()" /S 6 /D -
  45. ::#|
  46. ::#|  /S Int            : Supply the length of a Custom Pattern.
  47. ::#|
  48. ::#|  /D "Delim"        : Delim Character to use.
  49. ::#|  /D "Delim Delim"  : Supply Multiple Delims ; inserted after each corresponding iteration.
  50. ::#|  Delim Substitution: The following characters require substitution to use:
  51. ::#|                      - {FS} for / Forward-Slash ; {SC} for ; Semicolon
  52. ::#|                      - {AS} for * Asterisk      ; {CM} for , Comma
  53. ::#|                      - {EQ} for = Equals
  54. ::#|
  55. ::#|  /B "Delim Delim"  : Bookend the string with the supplied pair of Delims.
  56. ::#|
  57. ::#|  /O                : Output the Generated string to STDOUT
  58. ::#|
  59. ::#---------------------------------------------------------------------------------------------------
  60.  
  61. :# $rStr Switches. Defined outside macro for readability and maintainance.
  62.  Set $rStr[Switches]="V" "A" "P" "L" "S" "R" "D" "O" "Mod" "B" "Rep"
  63.  
  64.  Set $rStr=For %%n in (1 2)Do if %%n==2 (                              %\n: CAPTURE ARG STRING =%
  65.   Set "$rStr[Syntax]=%\E%[33m!$rStr[args]!"%\n%
  66.   For /F "Tokens=1,2 Delims==" %%G in ('Set "$rStr_" 2^^^> nul')Do Set "%%~G=" %\n: RESETS ALL MACRO INTERNAL VARS =%
  67.   If not "!$rStr[args]:* /=!" == "!$rStr[args]!" (                              %\n: BUILD $rStr.Args[!$rStr_arg[i]!] ARRAY IF ARGS PRESENT =%
  68.    Set "$rStr_leading.args=!$rStr[args]:*/=!"                                   %\n: SPLIT ARGS FROM SWITCHES =%
  69.    For /F "Delims=" %%G in ("!$rStr_leading.args!")Do Set "$rStr_leading.args=!$rStr[args]:/%%G=!"%\n%
  70.    Set ^"$rStr[args]=!$rStr[args]:"=!"                                          %\n: REMOVE DOUBLEQUOTES FROM REMAINING ARGSTRING - SWITCHES =%
  71.    Set "$rStr_arg[i]=0"                                                            %\n: ZERO INDEX FOR ARGS ARRAY =%
  72.    For %%G in (!$rStr_leading.args!)Do (                                           %\n: BUILD ARGS ARRAY =%
  73.     Set /A "$rStr_arg[i]+=1"%\n%
  74.     Set "$rStr_arg[!$rStr_arg[i]!]=%%~G"%\n%
  75.     For %%i in ("!$rStr_arg[i]!")Do (                                              %\n: SUBSTITUTE THE FOLLOWING POISON CHARACTERS =%
  76.      Set "$rStr_arg[%%~i]=!$rStr_arg[%%~i]:{SC}=;!"%\n%
  77.      Set "$rStr_arg[%%~i]=!$rStr_arg[%%~i]:{QM}=?!"%\n%
  78.      Set "$rStr_arg[%%~i]=!$rStr_arg[%%~i]:{FS}=/!"%\n%
  79.      Set "$rStr_arg[%%~i]=!$rStr_arg[%%~i]:{AS}=*!"%\n%
  80.      Set "$rStr_arg[%%~i]=!$rStr_arg[%%~i]:{EQ}==!"%\n%
  81.      Set ^"$rStr_arg[%%~i]=!$rStr_arg[%%~i]:{DQ}="!"%\n%
  82.   ))) Else (                                                                           %\n: IF NO ARGS REMOVE DOUBLEQUOTES FROM ARGSTRING - SWITCHES =%
  83.    Set ^"$rStr[args]=!$rStr[args]:"=!"%\n%
  84.    Set "$rStr_Arg[1]=!$rStr[args]!"%\n%
  85.    Set "$rStr_Arg[i]=1"%\n%
  86.   )%\n%
  87.   For /L %%L in (2 1 4)Do If "!$rStr_LastSwitch!" == "" (%\n%
  88.    If "!$rStr[args]:~-%%L,1!" == " " Set "$rStr_LastSwitch=_"%\n%
  89.    If "!$rStr[args]:~-%%L,1!" == "/" (                                              %\n: FLAG LAST SWITCH TRUE IF NO SUBARGS ; FOR SWITCHES UP TO 3 CHARCTERS LONG =%
  90.     For /F "Delims=" %%v in ('Set /A "%%L-1"')Do Set "$rStr_Switch[!$rStr[args]:~-%%v!]=true"%\n%
  91.     Set "$rStr[args]=!$rStr[args]:~0,-%%L!"%\n%
  92.     Set "$rStr_LastSwitch=_"%\n%
  93.    )%\n%
  94.   )%\n%
  95.   For %%G in ( %$rStr[Switches]% )Do If not "!$rStr[args]:/%%~G =!" == "!$rStr[args]!" (%\n: SPLIT AND ASSIGN SWITCH VALUES =%
  96.    Set "$rStr_Switch[%%~G]=!$rStr[args]:*/%%~G =!"%\n%
  97.    If not "!$rStr_Switch[%%~G]:*/=!" == "!$rStr_Switch[%%~G]!" (%\n%
  98.     Set "$rStr_Trail[%%~G]=!$rStr_Switch[%%~G]:*/=!"%\n%
  99.     For %%v in ("!$rStr_Trail[%%~G]!")Do (%\n%
  100.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]: /%%~v=!"%\n%
  101.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:/%%~v=!"%\n%
  102.     )%\n%
  103.     Set "$rStr_Trail[%%~G]="%\n%
  104.     If "!$rStr_Switch[%%~G]:~-1!" == " " Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:~0,-1!"%\n%
  105.     If "!$rStr_Switch[%%~G]!" == "" Set "$rStr_Switch[%%~G]=true"%\n%
  106.     If not "!$rStr_Switch[%%~G]!" == "" If not "!$rStr_Switch[%%~G]!" == "true" (%\n%
  107.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:{SC}=;!"%\n%
  108.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:{QM}=?!"%\n%
  109.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:{FS}=/!"%\n%
  110.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:{AS}=*!"%\n%
  111.      Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:{EQ}==!"%\n%
  112.      Set ^"$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:{DQ}="!"%\n%
  113.    ))%\n%
  114.    If "!$rStr_Switch[%%~G]:~-1!" == " " Set "$rStr_Switch[%%~G]=!$rStr_Switch[%%~G]:~0,-1!"%\n%
  115.    Set "$rStr[RV]="%\n%
  116.   )%\n: ACTION SWITCH ASSESSMENT BELOW. USE CONDITIONAL TESTING OF VALID SWITCHES OR ARGS ARRAY TO ENACT COMMANDS FOR YOUR MACRO FUNCTION =%
  117.   Set "$rStr_Var=$Rstr_RV"%\n%
  118.   If not "!$rStr_Switch[L]!" == "true" If not "!$rStr_Switch[L]!" == "" (%\n%
  119.    For %%G in ( Switch[L] Switch[B] Switch[D] Switch[Mod] )Do If not "!$rStr_%%G!" == "true" If not "!$rStr_%%G!" == "" (%\n%
  120.     Set "$rStr_%%G[i]=0"%\n%
  121.     For %%H in (!$rStr_%%G!)Do (%\n%
  122.      Set /A "$rStr_%%G[i]+=1"%\n%
  123.      Set "$rStr_%%G[!$rStr_%%G[i]!]=%%H"%\n%
  124.      For %%q in (Switch[B] Switch[D])Do if "%%q" == "%%G" (%\n%
  125.       Set "rStr_dSub=%%H"%\n%
  126.       Set "rStr_dSub=!rStr_dSub:{AS}=*!"%\n%
  127.       Set "rStr_dSub=!rStr_dSub:{SC}=;!"%\n%
  128.       Set "rStr_dSub=!rStr_dSub:{CN}=:!"%\n%
  129.       Set "rStr_dSub=!rStr_dSub:{CM}=,!"%\n%
  130.       Set "$rStr_%%G[!$rStr_%%G[i]!]=!rStr_dSub!"%\n%
  131.      )%\n%
  132.      If "%%G" == "Switch[Mod]" If not "%%H" == "0" Set /A "$rStr_Switch[L][!$rStr_%%G[i]!]=!random! %% %%H "%\n%
  133.    ))%\n%
  134.    If "!$rStr_Switch[P]!" == "" Set "$rStr_Switch[P]=ANM" %\n%
  135.    If "!$rStr_Switch[P]!" == "true" Set "$rStr_Switch[P]=ANM" %\n%
  136.    If /I "!$rStr_Switch[P]!" == "ANM" ( Set "$rStr_CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ^& Set "$rStr_pSize=62" )%\n%
  137.    If /I "!$rStr_Switch[P]!" == "ANU" ( Set "$rStr_CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ^& Set "$rStr_pSize=36" )%\n%
  138.    If /I "!$rStr_Switch[P]!" == "ANL" ( Set "$rStr_CharSet=abcdefghijklmnopqrstuvwxyz0123456789" ^& Set "$rStr_pSize=36" )%\n%
  139.    If /I "!$rStr_Switch[P]!" == "AM" ( Set "$rStr_CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ^& Set "$rStr_pSize=52" )%\n%
  140.    If /I "!$rStr_Switch[P]!" == "AL"  ( Set "$rStr_CharSet=abcdefghijklmnopqrstuvwxyz" ^& Set "$rStr_pSize=26" )%\n%
  141.    If /I "!$rStr_Switch[P]!" == "AU"  ( Set "$rStr_CharSet=ABCDEFGHIJKLMNOPQRSTUVWXYZ" ^& Set "$rStr_pSize=26" )%\n%
  142.    If /I "!$rStr_Switch[P]!" == "N"   ( Set "$rStr_CharSet=1234567890" ^& Set "$rStr_pSize=10" )%\n%
  143.    If /I "!$rStr_Switch[P]!" == "H"   ( Set "$rStr_CharSet=ABCDEF0123456789" ^& Set "$rStr_pSize=16" )%\n%
  144.    If not "!$rStr_Switch[P]:*]=!" == "!$rStr_Switch[P]!" (%\n: facilitate custom P [start-end] Pattern Range =%
  145.     For /F "Tokens=1,2 Delims=[-]" %%1 in ("!$rStr_Switch[P]!")Do (%\n%
  146.      Set "$rStr_pSize="^& Set "$rStr_Start=%%1"%\n%
  147.      For %%c in ( 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )Do (%\n%
  148.       If "%%c" == "%%1" (%\n%
  149.        Set "$rStr_CharSet=%%c"%\n%
  150.        Set "$rStr_pSize=0"%\n%
  151.       )%\n%
  152.       If "%%c" == "%%2" (%\n%
  153.        Set "$rStr_CharSet=!$rStr_CharSet!%%c"%\n%
  154.        Set "$rStr_Start="%\n%
  155.        Set /A "$rStr_pSize+=1"%\n%
  156.       )%\n%
  157.       If not "!$rStr_CharSet!" == "%%c" If not "!$rStr_Start!" == "" (%\n%
  158.        Set "$rStr_CharSet=!$rStr_CharSet!%%c"%\n%
  159.        Set /A "$rStr_pSize+=1"%\n%
  160.    ))))%\n%
  161.    If "!$rStr_CharSet!" == "" (%\n%
  162.     Set "$rStr_CharSet=!$rStr_Switch[P]!"%\n%
  163.     If not "!$rStr_Switch[S]!" == "" If not "!$rStr_Switch[S]!" == "true" (Set "$rStr_pSize=!$rStr_Switch[S]!")%\n%
  164.     If "!$rStr_pSize!" == "" (%\n%
  165.      For /L %%i in (0 1 200) Do If "!$rStr_pSize!" == "" If "!$rStr_Switch[P]:~%%i,1!" == "" ( Set /A "$rStr_pSize=%%i-1" )%\n%
  166.    ))%\n%
  167.    If "!$rStr_Switch[R]!" == "" Set "$rStr_Switch[R]=!$rStr_Switch[L][i]!"%\n%
  168.    If not "!$rStr_Switch[Rep]!" == "" (%\n%
  169.     Set "$rStr_Switch[D]=!$rStr_Switch[D]:{SC}=;!"%\n%
  170.     Set "$rStr_Switch[D]=!$rStr_Switch[D]:{CM}=,!"%\n%
  171.     Set "$rStr_Switch[D]=!$rStr_Switch[D]:{AS}=*!"%\n%
  172.     Set "$rStr_Switch[R]=!$rStr_Switch[Rep]!"%\n%
  173.     For /L %%i in (1 1 !$rStr_Switch[R]!)Do (%\n%
  174.      Set "$rStr_Switch[L][%%i]=!$rStr_Switch[L]!"%\n%
  175.      Set "$rStr_Switch[D][%%i]=!$rStr_Switch[D]!"%\n%
  176.      If "%%i" == "!$rStr_Switch[Rep]!" Set "$rStr_Switch[D][%%i]="%\n%
  177.    ))%\n%
  178.    For /L %%i in (1 1 !$rStr_Switch[R]!) Do (%\n%
  179.     FOR /L %%n IN (1 1 !$rStr_Switch[L][%%i]!)Do (%\n%
  180.      For /F "delims=" %%c In ( 'SET /A "!RANDOM! %% !$rStr_pSize!"' )Do (%\n%
  181.       Set "$rStr[RV]=!$rStr[RV]!!$rStr_CharSet:~%%c,1!"%\n%
  182.     ))%\n%
  183.     Set "$rStr[RV]=!$rStr[RV]!!$rStr_Switch[D][%%i]!"%\n%
  184.    )%\n%
  185.    Set "$rStr[RV]=!$rStr_Switch[B][1]!!$rStr[RV]!!$rStr_Switch[B][2]!"%\n%
  186.    If not "!$rStr_Switch[A]!" == "" ( Set "$rStr_Var=!$rStr_Switch[A]!" ^& For /F "Delims=" %%v in ("!$rStr_Switch[A]!")Do Set "$rStr[RV]=!%%v!!$rStr[RV]!")%\n%
  187.    If not "!$rStr_Switch[V]!" == "" ( Set "$rStr_Var=!$rStr_Switch[V]!" ^& Set "!$rStr_Switch[V]!=!$rStr[RV]!" 2^> nul ) %\n%
  188.    If "!$rStr_Switch[O]!" == "true" Echo(!$rStr[RV]!%\n%
  189.   )Else (Echo( /L Integer Required^^! )%\n%
  190.  ) Else Set $rStr[args]=
  191.  
  192. :# load the macro from another script
  193. If not "%~1" == "" Echo("%~1"|findstr.exe /IC:"/load" > nul && exit /B 0
  194.  
  195. :#Show help
  196.  If not "%~1" == "" Echo("%~1"|findstr.exe /IC:"/?" > nul && (
  197.   (For /F "Tokens=1,2* Delims=#" %%G in ('findstr.exe /BLIC:"::#" "%~f0"')Do Echo(%%H) >"%TEMP%\%~n0_help.txt"
  198.   Type "%TEMP%\%~n0_help.txt" | More
  199.   Goto :Eof
  200.  )
  201.  
  202. :# Enable $rStr Macro
  203.  Setlocal enableextensions enableDelayedExpansion
  204. If not "%~1" == "" (
  205.  %$rStr% %*
  206. )
  207.  
  208. If not "%~1" == "" (
  209.  If defined $rStr_Var (
  210.   For %%v in ("!$rStr[RV]!")Do Endlocal & Set "%$rStr_Var%=%%~v"
  211.  )
  212.  exit /b
  213. )
  214.  
  215. :# Usage Examples
  216. :# {3xINT:10to24xINT-5xINT}
  217.  %$rStr% /L 3 . 5 /Mod 0 10+15 0 /P N /B { } /D : - /V output1
  218.  
  219. :# append {7to15xLET;2xLET,7to15xLET} to {3xINT:10to24xINT-5xINT} to get: {3xINT:10to24xINT-5xINT}{7to15xLET;2xLET,7to15xLET}
  220.  %$rStr% /L . 2 . /Mod 7+9 0 3+4 /P AM /B { } /D {SC} {CM} /A output1 /O
  221.  
  222. :# 4xHEX;4xHEX;4xHEX
  223.  %$rStr% /L 4 /Rep 3 /P H /D {SC} /V output2
  224.  
  225. :# character range example: [3x[3-f]-3x[3-f]-3x[3-f]-3x[3-f]-3x[3-f]]
  226.  %$rStr% /L 3 /Rep 5 /P [3-f] /B [ ] /D - /V output3
  227.  
  228.  Set output
  229.  Timeout /T 2 /Nobreak > nul
  230.  pause
  231.  CLS
  232.  
  233. :# custom character set example - 'maze box' . Supply size of pattern using /S Integer
  234.  %$rStr% /l 25 /p - /O /S 1
  235.  For /L %%i in (1 1 12)Do %$rStr% /l 23 /P + x  . /B "| |" /O /S 6
  236.  %$rStr% /l 25 /p - /O /S 1
  237.  
  238.  Timeout /T 2 /Nobreak > nul
  239.  pause
  240.  CLS
  241.  
  242. Goto :Eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement