coldReactive

Windows - Random File Picker Batch File

Feb 10th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. :: Create numbered list of files in a temporary file
  5. set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
  6. dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
  7.  
  8. :: Count the files
  9. for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N
  10.  
  11. call :openRandomFile
  12.  
  13. :: Delete the temp file
  14. del "%tempFile%"
  15.  
  16. exit /b
  17.  
  18. :openRandomFile
  19. set /a "randomNum=(%random% %% cnt) + 1"
  20. for /f "tokens=1* delims=:" %%A in (
  21. 'findstr "^%randomNum%:" "%tempFile%"'
  22. ) do start "" "%%B"
  23. exit /b
Advertisement
Add Comment
Please, Sign In to add comment