I pick a folder in my file manager and type R and it plays a random file.
It filters some filetypes out which I don't want to launch but keeps the rest, and relies on the file association to pick the media player.
Code: Select all
@echo off
:: launch random file
set tempfile2="%temp%.\randfile.txt"
set "lines="
dir /b /a:-d |findstr /v /i /c:.txt /c:.htm /c:.html /c:.doc /c:.exe /c:.com /c:.bat /c:.cmd /c:.msi /c:.scr /c:.dll /c:.reg /c:.zip /c:.rar /c:.bas >%tempfile2% 2>nul
if errorlevel 1 (
echo No files to play in this folder
echo.
pause
goto :eof
)
for /f "delims=" %%a in ('find /c /v "" ^<%tempfile2%') do set lines=%%a
:start
set c=
:loop
set /a c+=1
set /a "rand=(%random%*%lines%)/32768+1"
if not %c% EQU 10 goto :loop
for /f "tokens=1* delims=]" %%a in (
'find /n /v "" ^<"%tempfile2%" ^|findstr /r ^^[%rand%]'
) do set "file=%%b"
start "" "%file%"
I was using Gawk to get the random number and then I noticed a post here viewtopic.php?p=15174#p15174 re copying random files
Aacini uses this to get a random number from 1 to n (lines/n is the number of lines in the list of files).
set /a "rand=(%random%*%lines%)/32768+1"
It seemed to always start with the same integer (6) when I tested it but letting it have several passes made it more random, so I settled on 10 passes.
Maybe someone else will find the batch file useful.