0

I'm trying to see if I can't make a random name generator using batch scripts (For fun basically). The idea is to produce a batchfile which creates pronounceable names instead of random text strings.

While I could easily make a simple system which picks a random name from a premade list, that's boring. What I want to do is have a system which selects a variable string of syllables and strings them into a name.

That works via:

set /a roll=%random%%%100+1
if %roll% leq 100 set syllable=ah
if %roll% leq 95 set syllable=roh

and so on, with a bit at the end that checks to see if a count is equal to a certain number, and if not ads 1 tot he count and loops back to roll for another syllable. The number of syllables is chosen via a similar modified %random% and ranges from 2 to 8.

Unfortunately I am stuck at getting the multiple uses of that loop to become a single variable. I think that you can use the type command to input text to a batch file from a text file, so what I want to do is work out a way to make

echo %syllable% >> name.txt

result in a text file which has all it's entries on a single line with no spaces, instead of one entry per line.

I would also appreciate someone showing me how to use the set command to make a variable equal a text file's contents. I think it would be:

set name=< name.txt

But that may actually make the variable be "< name.txt"...

Sean McLain
  • 317
  • 2
  • 3
  • 12
  • Probably setting your variable to the file name is best. Then, you can simply use code to extract the text from the file named in that variable. You should also consider doing this with [tag:powershell], instead. Much richer programming environment and much less awkward and frustrating to work with. – David Sep 09 '15 at 00:14
  • Unfortunately, I don't know how to use powershell at all. Or any other programming language. Plus if i can work this out in batch I can add this bit into the program I have which renames files in a folder for me, and therefore have pronounceable names for each file instead of "Gh17haM". I'd also rather solve this one singular problem than learn an entire new programming language. – Sean McLain Sep 09 '15 at 00:17
  • Does this help: http://stackoverflow.com/questions/134001/how-can-i-load-the-contents-of-a-text-file-into-a-batch-file-variable One of the answers says `set /p VAR1= – Jerry Jeremiah Sep 09 '15 at 00:41
  • @JerryJeremiah Yes, a lot! Thank you. – Sean McLain Sep 09 '15 at 00:42

2 Answers2

0

I omitted the @ECHO OFF on the first line on purpose. Please run this and examine the output. Does this look something like what you are trying to accomplish?

SETLOCAL ENABLEDELAYEDEXPANSION
SET /A ROLL=%RANDOM% %% 100

FOR /L %%i IN (1,1,10) DO (
    SET /A ROLL=!RANDOM! %% 100

    echo ROLL is now !ROLL!

    IF !ROLL! LEQ 100 SET NEWSYLL=ah
    IF !ROLL! LEQ 50 SET NEWSYLL=roh

    SET SYLLABLE=!SYLLABLE!!NEWSYLL!

    echo SYLLABLE is now !SYLLABLE!
)

ECHO %SYLLABLE%>olfile.txt
EXIT /B 0
lit
  • 14,456
  • 10
  • 65
  • 119
0

In your code, you first generate a random number between 1 and 100 and then choose first syllable if number is between 100 and 96 (?), second syllable if number is between 95 (and 90?) etc. This method is cumbersome. It is much simpler to generate a random number between 1 and the number of syllables and then directly get one.

The program below use an array to directly get each random syllable; a detailed description of array management in Batch files is given at Arrays, linked lists and other data structures in cmd.exe (batch) script. Each syllable is joined in a single variable using Delayed Expansion.

@echo off
setlocal EnableDelayedExpansion

rem Define the syllable array
set N=0
for %%a in (ah1 roh1 ah2 roh2 etc) do (
   set /A N+=1
   set "syllable[!N!]=%%a"
)

rem Get a random number of syllables (from 2 to 8)
set /A len=%random% %% 7 + 2

rem Generate the name
set "name="
for /L %%i in (1,1,%len%) do (
   set /A num=!random! %% N + 1
   for %%n in (!num!) do set "name=!name!!syllable[%%n]!"
)

echo Name: "%name%"
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108