Friedslick6

Line Randomiser

Apr 7th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.22 KB | None | 0 0
  1. @REM Only display the command output on screen
  2. @ECHO OFF
  3. @REM Set the command window title
  4. TITLE Line Randomiser
  5. @REM Change the directory to that of the batch file's location.
  6. CD /D %~dp0%
  7. @REM Set each variable to be expanded at execution time.
  8. SETLOCAL ENABLEDELAYEDEXPANSION
  9. @REM Set the text file variable to the first command line argument (drag-and-drop).
  10. SET LIST=%~1
  11.  
  12. :INPUT1
  13. @REM If the text file variable is not defined, prompt the user for the text file variable.
  14. IF NOT DEFINED LIST SET /P LIST=Please type the name of the text file to parse:
  15. @REM If the text file variable is not defined, return to the text file variable input label.
  16. IF NOT DEFINED LIST GOTO :INPUT1
  17. @REM Remove potential double quotes from the text file variable.
  18. SET LIST=%LIST:"=%
  19. @REM Append a .txt extension to the text file variable if needed.
  20. IF /I NOT %LIST:~-4%==.txt SET LIST=%LIST%.txt
  21. @REM If the referenced text file doesn't exist,
  22. IF NOT EXIST "%LIST%" (
  23.    @REM direct an error message to the command output
  24.     ECHO The referenced text file ^("%LIST%"^) does not exist.
  25.    @REM reset the text file variable for looping
  26.     SET LIST=
  27.    @REM and return to the text file variable input label.
  28.     GOTO :INPUT1
  29. )
  30. @REM Find and set the line total variable from the referenced text file.
  31. FOR /F %%A IN ('FIND /V /C""^<"%LIST%"') DO SET TOTAL=%%A
  32.  
  33. :INPUT2
  34. @REM Reset the line output number and validation variables for looping.
  35. FOR %%A IN (NUMBER TEST) DO SET %%A=
  36. @REM Prompt the user for the line output number variable.
  37. SET /P NUMBER=Please set the number of lines for output:
  38. @REM If the line output number variable is not defined, return to the line output number variable input label.
  39. IF NOT DEFINED NUMBER GOTO :INPUT2
  40. @REM Loop the line output number variable through the numeric range to check its validity.
  41. FOR /F "delims=0123456789" %%A IN ("%NUMBER%") DO SET TEST=%%A
  42. @REM If the validation variable includes text,
  43. IF DEFINED TEST (
  44.    @REM direct an error message to the command output
  45.     ECHO The line output number variable ^(%NUMBER%^) is non-numeric.
  46.    @REM and return to the line output number variable input label.
  47.     GOTO :INPUT2
  48. )
  49. @REM If the line output number variable is greater than the line total variable,
  50. IF %NUMBER% GTR %TOTAL% (
  51.    @REM direct an error message to the command output
  52.     ECHO The line output number variable ^(%NUMBER%^) exceeds the line total variable ^(%TOTAL%^) from the referenced text file ^("%LIST%"^).
  53.    @REM and return to the line output number input label.
  54.     GOTO :INPUT2
  55. )
  56. @REM Prompt the user to save the following lines of output.
  57. CHOICE /N /M "Save output? [Y]es/[N]o: "
  58. @REM If the user pressed the Y key,
  59. IF NOT ERRORLEVEL 2 (
  60.    @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,
  61.     SET OUTPUT=^>^>"%LIST:~,-4%-random%NUMBER%.txt"
  62.    @REM prompt the user on whether to overwrite that text file or not if it already exists,
  63.     IF EXIST !OUTPUT:~2! CHOICE /N /M "Overwrite file? [Y]es/[N]o: "
  64.    @REM and delete that text file if the user pressed the Y key,
  65.     IF NOT ERRORLEVEL 2 DEL !OUTPUT:~2!
  66. )
  67. @REM Clear the screen.
  68. CLS
  69. @REM For a range of numbers counting up from one to the line output number variable, send that number to the parsing label
  70. FOR /L %%A IN (1,1,%NUMBER%) DO CALL :PARSE %%A
  71. @REM Insert a newline.
  72. ECHO/
  73. @REM Pause the batch file execution, null its message and send a new message to the command output.
  74. <NUL SET/P="Press any key to restart . . . "&PAUSE>NUL
  75. @REM Reset the text file and redirection variables for looping.
  76. FOR %%A IN (LIST OUTPUT) DO SET %%A=
  77. @REM Clear the screen.
  78. CLS
  79. @REM Return to the text file variable input label.
  80. GOTO :INPUT1
  81.  
  82. :PARSE
  83. @REM Set the target line variable to a random number between one and the total number of lines in the referenced text file.
  84. SET/A TARGET=%RANDOM%*%TOTAL%/32768+1
  85. @REM Set a new cache variable to the target line variable each loop
  86. SET CACHE%1=%TARGET%
  87. @REM If there is more than one cache variable,
  88. IF DEFINED CACHE2 (
  89.    @REM for a range of numbers counting up from one to the line output number variable,
  90.     FOR /L %%A IN (1,1,%NUMBER%) DO (
  91.        @REM if that cached number variable exists,
  92.         IF NOT "!CACHE%%A!" == "" (
  93.            @REM if that number is not the current parse loop number,
  94.             IF NOT %%A EQU %1 (
  95.                @REM and if the target line variable matches that cached number variable, return to the parsing label.
  96.                 IF %TARGET% EQU !CACHE%%A! GOTO :PARSE
  97.             )
  98.         )
  99.     )
  100. )
  101. @REM For each line number and line in the text file,
  102. FOR /F "tokens=1,* delims=:" %%A IN ('FINDSTR /N /R ".*" "%LIST%"') DO (
  103.    @REM if that line number item matches the target line variable,
  104.     IF %%A EQU %TARGET% (
  105.        @REM set the current line variable to that line
  106.         SET LINE=%%B
  107.        @REM and break the loop by jumping to the output label.
  108.         GOTO :OUTPUT
  109.     )
  110. )
  111.  
  112. :OUTPUT
  113. @REM Direct the current line output number and line variable to the command output.
  114. ECHO %1 %LINE%
  115. @REM Direct the current line output number and line variable into the redirection variable if it exists.
  116. IF DEFINED OUTPUT ECHO %1 %LINE%%OUTPUT%
Advertisement
Add Comment
Please, Sign In to add comment