5

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?

4
  • Comment out @echo off and look at what your batch file is doing.
    – DavidPostill
    Commented Aug 23, 2017 at 22:46
  • 1
    Debugging your batch files
    – DavidPostill
    Commented Aug 23, 2017 at 22:46
  • @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. Commented Aug 23, 2017 at 22:58
  • Link fixed.....
    – DavidPostill
    Commented Aug 24, 2017 at 7:51

1 Answer 1

5

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 "
3
  • Wow, great catch, thanks a lot for this. I knew it was going to be as simple as this, I really shouldn't have missed it. Out of interest, is there any particular reason you used "%~1"? Since the addition of the ~ just expands the variable without quotes, could you not just reference the variable without quotes in the first place, with %1? I've tested the script using %1 and confirmed it works, so I'm wondering whether there are any downsides to using it that you had in mind? Commented Aug 25, 2017 at 0:07
  • 1
    It's just a habit - if not knowing exactly what is passed. If always enclosing in quotes they might be doubled so enclosing and stripping eventually present quotes ensures the new passed value is quoted.
    – LotPings
    Commented Aug 25, 2017 at 0:30
  • 1
    I like that habit with the tilde... I use that most of the time myself just in case as I've never run into a problem with it but have with not having it. +1 Commented Aug 28, 2017 at 23:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .