Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @REM Only display the command output on screen
- @ECHO OFF
- @REM Set the command window title
- TITLE Line Randomiser
- @REM Change the directory to that of the batch file's location.
- CD /D %~dp0%
- @REM Set each variable to be expanded at execution time.
- SETLOCAL ENABLEDELAYEDEXPANSION
- @REM Set the text file variable to the first command line argument (drag-and-drop).
- SET LIST=%~1
- :INPUT1
- @REM If the text file variable is not defined, prompt the user for the text file variable.
- IF NOT DEFINED LIST SET /P LIST=Please type the name of the text file to parse:
- @REM If the text file variable is not defined, return to the text file variable input label.
- IF NOT DEFINED LIST GOTO :INPUT1
- @REM Remove potential double quotes from the text file variable.
- SET LIST=%LIST:"=%
- @REM Append a .txt extension to the text file variable if needed.
- IF /I NOT %LIST:~-4%==.txt SET LIST=%LIST%.txt
- @REM If the referenced text file doesn't exist,
- IF NOT EXIST "%LIST%" (
- @REM direct an error message to the command output
- ECHO The referenced text file ^("%LIST%"^) does not exist.
- @REM reset the text file variable for looping
- SET LIST=
- @REM and return to the text file variable input label.
- GOTO :INPUT1
- )
- @REM Find and set the line total variable from the referenced text file.
- FOR /F %%A IN ('FIND /V /C""^<"%LIST%"') DO SET TOTAL=%%A
- :INPUT2
- @REM Reset the line output number and validation variables for looping.
- FOR %%A IN (NUMBER TEST) DO SET %%A=
- @REM Prompt the user for the line output number variable.
- SET /P NUMBER=Please set the number of lines for output:
- @REM If the line output number variable is not defined, return to the line output number variable input label.
- IF NOT DEFINED NUMBER GOTO :INPUT2
- @REM Loop the line output number variable through the numeric range to check its validity.
- FOR /F "delims=0123456789" %%A IN ("%NUMBER%") DO SET TEST=%%A
- @REM If the validation variable includes text,
- IF DEFINED TEST (
- @REM direct an error message to the command output
- ECHO The line output number variable ^(%NUMBER%^) is non-numeric.
- @REM and return to the line output number variable input label.
- GOTO :INPUT2
- )
- @REM If the line output number variable is greater than the line total variable,
- IF %NUMBER% GTR %TOTAL% (
- @REM direct an error message to the command output
- ECHO The line output number variable ^(%NUMBER%^) exceeds the line total variable ^(%TOTAL%^) from the referenced text file ^("%LIST%"^).
- @REM and return to the line output number input label.
- GOTO :INPUT2
- )
- @REM Prompt the user to save the following lines of output.
- CHOICE /N /M "Save output? [Y]es/[N]o: "
- @REM If the user pressed the Y key,
- IF NOT ERRORLEVEL 2 (
- @REM set the output variable as a redirection to another text file with the original's name and "-random" and the line output number appended,
- SET OUTPUT=^>^>"%LIST:~,-4%-random%NUMBER%.txt"
- @REM prompt the user on whether to overwrite that text file or not if it already exists,
- IF EXIST !OUTPUT:~2! CHOICE /N /M "Overwrite file? [Y]es/[N]o: "
- @REM and delete that text file if the user pressed the Y key,
- IF NOT ERRORLEVEL 2 DEL !OUTPUT:~2!
- )
- @REM Clear the screen.
- CLS
- @REM For a range of numbers counting up from one to the line output number variable, send that number to the parsing label
- FOR /L %%A IN (1,1,%NUMBER%) DO CALL :PARSE %%A
- @REM Insert a newline.
- ECHO/
- @REM Pause the batch file execution, null its message and send a new message to the command output.
- <NUL SET/P="Press any key to restart . . . "&PAUSE>NUL
- @REM Reset the text file and redirection variables for looping.
- FOR %%A IN (LIST OUTPUT) DO SET %%A=
- @REM Clear the screen.
- CLS
- @REM Return to the text file variable input label.
- GOTO :INPUT1
- :PARSE
- @REM Set the target line variable to a random number between one and the total number of lines in the referenced text file.
- SET/A TARGET=%RANDOM%*%TOTAL%/32768+1
- @REM Set a new cache variable to the target line variable each loop
- SET CACHE%1=%TARGET%
- @REM If there is more than one cache variable,
- IF DEFINED CACHE2 (
- @REM for a range of numbers counting up from one to the line output number variable,
- FOR /L %%A IN (1,1,%NUMBER%) DO (
- @REM if that cached number variable exists,
- IF NOT "!CACHE%%A!" == "" (
- @REM if that number is not the current parse loop number,
- IF NOT %%A EQU %1 (
- @REM and if the target line variable matches that cached number variable, return to the parsing label.
- IF %TARGET% EQU !CACHE%%A! GOTO :PARSE
- )
- )
- )
- )
- @REM For each line number and line in the text file,
- FOR /F "tokens=1,* delims=:" %%A IN ('FINDSTR /N /R ".*" "%LIST%"') DO (
- @REM if that line number item matches the target line variable,
- IF %%A EQU %TARGET% (
- @REM set the current line variable to that line
- SET LINE=%%B
- @REM and break the loop by jumping to the output label.
- GOTO :OUTPUT
- )
- )
- :OUTPUT
- @REM Direct the current line output number and line variable to the command output.
- ECHO %1 %LINE%
- @REM Direct the current line output number and line variable into the redirection variable if it exists.
- IF DEFINED OUTPUT ECHO %1 %LINE%%OUTPUT%
Advertisement
Add Comment
Please, Sign In to add comment