Get random line from textfile in batch (BATCH)
Imthedude025 Sep 21st, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- @ECHO OFF
- SETLOCAL EnableDelayedExpansion EnableExtensions
- REM Source file.
- REM The first line on this file should be blank as it will never be selected.
- REM Additionally, this file should have no empty lines on the end.
- SET "TextFile=q27829742.txt"
- REM Determine the number of lines.
- FOR /f %%a IN ('type "%textfile%"^|find /c /v ""') DO SET /a numlines=%%a
- REM Pick a random line.
- SET /A RandomLine=(%RANDOM% %% %NumLines%)
- REM Prevent skipping all the lines.
- IF "%RandomLine%"=="0" (SET "RandomLine=") ELSE (SET "RandomLine=skip=%randomline%")
- REM Print the random line.
- FOR /F "usebackq tokens=* %RandomLine% delims=" %%A IN (`TYPE %TextFile%`) DO (
- ECHO %%A
- REM We are done. Stop the script.
- GOTO Finish
RAW Paste Data