LabRat
LabRat

Reputation: 2014

Random line of text using batch

How could one select a random line of text from a text file and set it in a variable to use?

Upvotes: 1

Views: 5574

Answers (3)

Andriy M
Andriy M

Reputation: 77707

Here's yet another approach. It reads the file name from the command line and uses a FOR /L loop to get to the calculated line number:

@ECHO OFF
FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
SET /A skip=%RANDOM%%%lines
<%1 (
  FOR /L %%I IN (1,1,%skip%) DO (
    SET /P line=
  )
  SET line=
  SET /P line=
)
ECHO(%line%

The FOR /F loop simply gets the number of lines in the file (the method is borrowed from @Aacini's answer).

A rather simplistic formula then calculates the number of lines to skip in the file.

Next, the file is read. The FOR /L loop merely consumes the specified number of lines using a SET /P instruction. Following the loop, one more SET /P command reads the line that is eventually ECHOed.

The above implementation is just to show the basic idea. It is not without issues, but some of them could easily be resolved:

  1. There's no testing whether the parameter is indeed supplied. If it is absent, the script will break. You could add the necessary check at the beginning of the script like this:

    IF "%~1"=="" GOTO :EOF
    

    If there's no parameter, this command terminates the script by sending the control to the end of the script (GOTO :EOF).

  2. The file specified might not exist. Again, you could test that at the beginning, just after verifying that the parameter is supplied, to terminate the script if necessary:

    IF NOT EXIST %1 GOTO :EOF
    
  3. If the file is empty, the lines will be 0 and the subsequent expression using it will run into a division by zero error. Therefore, you'll also need to test the resulting line count (and prevent the script from running further if the count is indeed 0). You can do that by adding the following line just after the FOR /F loop:

    IF %lines%==0 GOTO :EOF
    
  4. Like I said, the formula is somewhat simplistic. It doesn't produce a number greater than 32767, which is the limitation of %RANDOM%. That might well be enough for you, but in case it is not, you could extend the range to 230-1 using two %RANDOM% calls like this:

    SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
    

So, here's the same script again, amended to address the above mentioned issues:

@ECHO OFF
IF "%~1"=="" GOTO :EOF
IF NOT EXIST %1 GOTO :EOF
FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
IF %lines%==0 GOTO :EOF
SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
<%1 (
  FOR /L %%I IN (1,1,%skip%) DO (
    SET /P line=
  )
  SET line=
  SET /P line=
)
ECHO(%line%

One other note is that, if you like, you can add messages explaining the reason for the premature termination of the script. Basically, wherever you want to add the message, you'll just need to replace the single

GOTO :EOF

with

(ECHO your message & GOTO :EOF)

For instance:

IF NOT EXIST %1 (ECHO Error! File not found & GOTO :EOF)

Upvotes: 1

Aacini
Aacini

Reputation: 67226

The Batch program below is Eitan's solution slightly modified to run faster:

@echo off
setlocal EnableDelayedExpansion
set INPUT_FILE="test.txt"

:: # Count the number of lines in the text file and generate a random number
for /f "usebackq" %%a in (`find /V /C "" ^< %INPUT_FILE%`) do set lines=%%a
set /a randnum=%RANDOM% * lines / 32768 + 1, skiplines=randnum-1

:: # Extract the line from the file
set skip=
if %skiplines% gtr 0 set skip=skip=%skiplines%
for /f "usebackq %skip% delims=" %%a in (%INPUT_FILE%) do set "randline=%%a" & goto continue
:continue

echo Line #%randnum% is:
echo/!randline!

Upvotes: 3

Eitan T
Eitan T

Reputation: 32930

Like it is already mentioned here in StackOverflow, among others, %RANDOM% expands to a random number between 0 and 32767.

You can use this mechanism to generate a random line number. However, to make it a valid line number you will have to normalize it by the the number of lines in the input text file.

Here's a simple script that shows how to do it:

@echo off
setlocal enabledelayedexpansion
set INPUT_FILE="test.txt"

:: # Count the number of lines in the text file and generate a random number
set lines=0
for /f "usebackq" %%a in (%INPUT_FILE%) do set /a lines+=1
echo %RANDOM% >nul
set /a randnum=%RANDOM% * !lines! / 32768 + 1

:: # Extract the line from the file
set lines=0
for /f "usebackq tokens=*" %%a in (%INPUT_FILE%) do (
    set /a lines+=1
    if !lines!==!randnum! set randline=%%a
)

echo Line #!randnum! is:
echo.!randline!

Upvotes: 2

Related Questions