Windows – Play random movie/episode within a folder from its context menu

batch filecontext menurandom number generatorwindowswindows-explorer

The Goal

I have a relatively expansive media collection stored locally on my machine within two parent folders ("Movies" and "Shows"). My goal is to be able to play at random any one of the movies or episodes I have within these folders by right-clicking the parent folder and selecting a "Play random movie/episode" item from the extended context menu.

The Batch Script

I have the following batch script stored in a folder on my Windows 7 machine, which I took from here and adapted to look for .mp4, .mkv and .avi files (all of the extensions that my media collection consists of):

@echo off   
setlocal

:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir *.mp4,*.mkv,*.avi /b /s /a-d %1 | findstr /n "^" >"%tempFile%" & REM "

:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N
call :openRandomFile

:: Delete the temp file
del "%tempFile%"
exit /b

:openRandomFile
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
  'findstr "^%randomNum%:" "%tempFile%"'
) do start "" "%%B"

exit /b

The Context Menu Item

To add an extended context menu item to run the above batch file, I created the following registry keys, which results in a context menu item on folders that runs the batch file at the path specified when clicked.

[HKEY_CLASSES_ROOT\Directory\shell\Play random movie/episode]
"Extended"=""
"Position"="Top"

[HKEY_CLASSES_ROOT\Directory\shell\Play random movie/episode\command]
@="\"B:\\Users\\Hashim\\Resources\\Windows Modding\\Play Random.bat\" \"%1\""

Where I'm Stuck

The registry key seems to work well enough – the item shows up in the extended context menu for folders:

enter image description here

…and when clicked, it runs the batch file. The problems lie in the execution of the script.

  1. Instead of selecting just the .mkv, .mp4 and .avi file formats, the script selects all file types.

  2. Instead of searching, listing and selecting from files within the target folder – the folder that the script is selected to run on from its right-click context menu – it instead runs on what appears to be the parent folder of the target folder and all subfolders. Because the target folder in this case is under the user folders directory, this essentially means that it randomly selects files from the entire user drive.

These problems persist in spite of the fact that the script uses dir *.mp4,*.mkv,*.avi /b /s /a-d %1 to enumerate the files, where %1 is the parameter for the target folder.

Why isn't the script working as expected? What have I missed here?

Best Answer

Point 1 from your conclusion :

Instead of selecting just the .mkv, .mp4 and .avi file formats, the script selects all file types.

Is clear from the command

dir *.mp4,*.mkv,*.avi /b /s /a-d %1

This initiates 4 scans, 3 from current directory with the respective extensions and one for the passed %1 value

I suggest you first do a CD /D "%~1" to have the dir working from that location.

@echo off   
setlocal
CD /D "%~1"
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir /b /s /a-d *.mp4,*.mkv,*.avi | findstr /n "^" >"%tempFile%" & REM "
Related Question