Zebus
Zebus

Reputation: 3

Creating a word list using batch

I want to make a batch file that prints out to a text file a list of combination of numbers and letters.
Using {0, ..., 9, a, ..., z, A, ..., Z} as my character pool, I have 62 unique characters.
The word length starts as 1 and increases up to a predetermined value.

The script starts at length = 1 and prints out 0 to Z.
Then it proceeds to length = 2 and prints out 00 to ZZ, and so on...

Upvotes: 0

Views: 4619

Answers (2)

Aacini
Aacini

Reputation: 67216

Perhaps this is what you want?

TEST.BAT

@echo off
set charPool=_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set charLen=62
(for /L %%a in (1,1,%1) do (
   set permutation=
   call :makePermutation %%a
)) > textfile.txt
goto :EOF

:makePermutation level
setlocal EnableDelayedExpansion
set lastPermutation=%permutation%
for /L %%i in (1,1,%charLen%) do (
   set permutation=!lastPermutation!!charPool:~%%i,1!
   if %1 gtr 1 (
      set /A newLevel=%1-1
      call :makePermutation !newLevel!
   ) else (
      echo(!permutation!
   )
)
exit /B

The batch file must be started with a number as parameter which is the unit length.

For example on using TEST.BAT 1 the text file textfile.txt contains 62 lines.

Note that TEST.BAT 2 generates 3906 combinations (strictly speaking, permutations in statistical sense) from 0 to ZZ, and TEST.BAT 3 generates 242234 combinations from 0 to ZZZ!

Calculation example for estimating the number of strings in text file (size of file):

Running TEST.BAT with 5 as parameter produces

62 ^ 5 + 62 ^ 4 + 62 ^ 3 + 62 ^ 2 + 62 ^ 1 = 931.151.402

strings in text file.

Upvotes: 0

dbenham
dbenham

Reputation: 130819

Here is an iterative solution that is much faster.

No need for CALL. Each permutation is only generated once.

I was able to generate up to length 4 with over 15 million permutations in less than 5 minutes.

@echo off
setlocal enableDelayedExpansion
set chars=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set maxPos=61
del output.txt 2>nul

>prior.txt echo(""
for /l %%I in (1 1 %1) do (
  >new.txt (
    for /f %%A in (prior.txt) do for /l %%N in (0 1 %maxPos%) do echo(%%~A!chars:~%%N,1!
  )
  type new.txt>>output.txt
  move /y new.txt prior.txt >nul
)
del prior.txt

Upvotes: 2

Related Questions