I am trying to create a batch file that will loop a random number of times between 2 and 10 and pull a random line from a text file each time and display it.
set var1=%RANDOM%
set /a var2=(var1*9/32768)+2
for /l %%x in (1,1,%var2%) do (
set "lines=0"
for /f "tokens=*" %%a in (c:\myfile.txt) do set /a "lines+=1"
set /a "skip=%var1% %% lines"
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%")
for /f "skip=%skip% tokens=*" %%a in (c:\myfile.txt) do set "item=%%a"&goto display
:display
echo %item%
)
This is two different pieces of code that each work on their own, but I am having trouble putting them together.
The code below will display a random line from the file every time I run it.
set "lines=0"
for /f "tokens=*" %%a in (c:\myfile.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 (c:\myfile.txt) do set "item=%%a"&goto display
:display
echo %item%
And this code will display 1 between 2 and 10 times.
@echo off
set var1=%RANDOM%
set /a var2=(var1*9/32768)+2
for /l %%x in (1,1,%var2%) do (
echo 1
)
I'm probably just overlooking the obvious, but I don't do a lot of batch scripting.