Need help setting an r.n.g.

September 16, 2017 at 06:04:05
Specs: Windows 10
How can I make it so that, it randomly picks 1 line off of the .txt file?
Thanks in advanced!!

@echo off
SETLOCAL EnableDelayedExpansion EnableExtensions
:start over
cls
echo -random (gives you a random line from your .txt file)
echo -create (creates a .txt file to save your games too)
echo.
set /p this= What do you want to do?:
goto %this%

:create
cls
set /p that= what do you want the name of your .txt to be?:
fsutil file createnew %that%.txt 0 >nul
pause
if exist %that%.txt (
echo Success
pause
goto :start over
) else (
echo Failed
pause
goto :create
)

:random
cls
set /p name= What is the name of your .txt file?:
SET TextFile=%name%.txt
set /p numlines= How many lines do you have on your .txt file?:

FOR /F "usebackq tokens=* delims=" %%A IN (`TYPE %TextFile%`) DO SET /A %numlines%+1

SET /A RandomLine=(%RANDOM% %% %numlines) +1

IF "%RandomLine%"=="%numlines%" SET RandomLine=2

FOR /F "usebackq tokens=* skip=%RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
ECHO %%A
)
pause

ENDLOCAL

cls
echo -finish
echo -start over
echo.
set /p program= What do you want to do?:
goto %program%

:finish
echo Good Bye!!
timeout /t 2 /nobreak >nul
exit


See More: Need help setting an r.n.g.

Reply ↓  Report •

#1
September 17, 2017 at 08:41:03
::====== script starts here ===============
::
:: randline.bat 2017-09-17 8:35:45.98
@echo off & setLocal enableDELAYedeXpansioN

set/p T= How many lines in the file?
set/a R=!RANDOM!%%!T!
set/a N=0

for /f "tokens=* delims= " %%a in (myfile) do (
if !N! equ !R! echo.%%a && goto :eof
set/a N+=1
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

#2
September 17, 2017 at 08:51:08
It didn't work, I don't know if I did something wrong, but it didn't work.

Reply ↓  Report •

#3
September 17, 2017 at 21:54:25
Based on M2's script but with auto-line counter, and some debugging stuff left in::

:: ---- begin script
@echo off & setLocal enableDELAYedeXpansioN
for /f "tokens=1 delims=[]" %%a in ('find /n /v "" ^<myfile') do set T=%%a
echo lines counted: %T%
set/a R=!RANDOM!%%!T!
echo line selected %R%
for /f "tokens=*" %%a in ('more +%R% myfile') do (
echo %%a
goto :eof
)
:: ------- end script
NOTE that 'echo' can have problems with "special" characters. BEWARE. If any doubt, put %%a in dbl-quotes to output and handle/remove them later.

::====== script ends here =================


Reply ↓  Report •

Related Solutions

#4
September 18, 2017 at 00:27:39
It doesn't help to say it didn't work.

It might help to say what it did or didn't did.

Did you paste it into a text file and give it a clever name?
RANDLINE.BAT might be a good choice.
Did you type the name of the bat at the prompt and press enter?
Did you get asked for input?.
Did you get an error?
Do you HAVE a text file called myfile in that directory?

=====================

M2 Get custom script or take private lessons


Reply ↓  Report •

#5
September 18, 2017 at 14:04:05
Yes, I did make a text file called myfile, I copy pasted the script you posted, and when it opened I put the lines of the text file and then it closed.

Reply ↓  Report •

Ask Question