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
.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:
...and when clicked, it runs the batch file. The problems lie in the execution of the script.
Instead of selecting just the
.avifile formats, the script selects all file types.
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.