1

Im working on creating a batch file that will make a fake profile from a list of names and generate random dates of birth. I have the dates of birth down but when I try to run this code, the out put for %FName% is always (Zachary) the last line of my text file, and %LName% always equals Dora, the last line of the other file. I do not know why this is happening. Ive looked at this but I cant seem to find the problem. The list of names is a simple:

Bob
Billy
Jacob
Andrew
Vince

Here is the code:

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:start
:FName
cls
set "lines=0" 
for /f "tokens=*" %%a in (GivenNames.txt) do set /a "lines+=1" 
set /a "skip=%random% %% lines" 
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%") 
for /f "%skip% tokens=*" %%a in (GivenNames.txt) do set "FName=%%a"
goto LName

:LName
cls
set "lines=0" 
for /f "tokens=*" %%a in (Surnames.txt) do set /a "lines+=1" 
set /a "skip=%random% %% lines" 
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%") 
for /f "%skip% tokens=*" %%a in (Surnames.txt) do set "LName=%%a"
goto MName

:MNAme
cls
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set middle=
FOR /L %%b IN (0, 1, 16) DO (
SET /A rnd_num=!RANDOM! * 26 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set MName=!pwd!%%c
)

goto compilename

:compilename
cls
set fullname=%FName% %MName%. %LName%
cls
echo %fullname%
pause>nul
goto start

Example output:

Zachary I. Dora

What I need:

"Random First Name From List" "Random Letter". "Random Last Name From List"

Thanks in advance, -Zackary

3 Answers3

0
for /f "%skip% tokens=*" %%a in (GivenNames.txt) do set "FName=%%a"&goto LName

%%a will be assigned each line-value from the file in turn after skipping the first (skipvalue) lines.

The variable will thus be assigned each value from %%a in turn and hence will end up with the last value assigned (the last-in-file)

The & separates two separate commands, so the assignment is made to the first line (after the number skipped) and having made the assignment, execution is passed to the label so no further assignment is made as the loop is terminated.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

When for always reads in a file, it always starts somewhere and keeps reading until it reaches the end. This is why FName and LName are always the last line of your text files.

You can manipulate set /p to read in a specific line of a text file by reading in the previous lines and storing them in a dummy variable.

:FName
cls
set "lines=0" 
for /f "tokens=*" %%a in (GivenNames.txt) do set /a "lines+=1" 
set /a "skip=!random! %% lines"
(
    for /l %%B in (0,1,!skip!) do set /p ".="
    set /p "FName="
)<GivenNames.txt
goto LName

:LName
cls
set "lines=0" 
for /f "tokens=*" %%a in (Surnames.txt) do set /a "lines+=1" 
set /a "skip=!random! %% lines"
(
    for /l %%B in (0,1,!skip!) do set /p ".="
    set /p "LName="
)<Surnames.txt
goto MName
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
0

May I suggest you a different approach? You may use an array to load the lines of the file in variables in the first loop, so the selection of the random name does not require to read the file again. In this problem the two methods works pretty much the same, but if you need to generate a large amount of random names, the processing of array elements would be much faster than the repeating reading of a file.

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:start

:FName
cls
set "lines=0" 
for /f "tokens=*" %%a in (GivenNames.txt) do (
   set "name[!lines!]=%%a"
   set /a "lines+=1"
)
set /a "skip=!random! %% lines"
set "FName=!name[%skip%]!

:LName
set "lines=0" 
for /f "tokens=*" %%a in (Surnames.txt) do (
   set "name[!lines!]=%%a"
   set /a "lines+=1" 
)
set /a "skip=!random! %% lines"
set "LName=!name[%skip%]!

:MNAme
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set /A rnd_num=%RANDOM% * 26 / 32768
set "MName=!alfanum:~%rnd_num%,1!"

:compilename
set fullname=%FName% %MName%. %LName%
echo %fullname%

For further details on array management in Batch files, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108