4
1
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:
...and when clicked, it runs the batch file. The problems lie in the execution of the script.
Instead of selecting just the
.mkv
,.mp4
and.avi
file 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.
Comment out
@echo off
and look at what your batch file is doing. – DavidPostill – 2017-08-23T22:46:19.350Debugging your batch files – DavidPostill – 2017-08-23T22:46:37.167
@DavidPostill - I've spent time debugging it, it's how I got from the last iteration of the question to this one. I'd actually done it before you posted your comments on the last question but needed some time to sleep. This current iteration is a result of all the debugging that I've been able to do, and I'm now leaving it to SU to help me figure out what I've not been able to. Also just as an FYI, your link to the Rob van der Woude site doesn't work as that site doesn't use
https
. – Hashim – 2017-08-23T22:58:08.427Link fixed..... – DavidPostill – 2017-08-24T07:51:27.433