Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder full of videos, and would like to assign a script to a shortcut that I can click to open one at random in MPC-HC. I am running Windows 7.

Ideally, it would just be a script to open a random file in the folder in which the script file is placed. Then I could just place one in any folder I wanted, and set a shortcut to the script.

share|improve this question
What are the extensions of the video files you would want to have picked at random? – Richard Feb 4 '13 at 22:24

Yeah, that sounds like fun. :-)

Make a file named PlayRandom.bat and paste the following script into it.

@Echo Off
SetLocal EnableDelayedExpansion
CD /D %~dp0
Set Count=0
For /F "tokens=*" %%f In ('Dir /A-D /B *.txt') Do (Set /A Count=!Count!+1)
Set /A Skip=%Random%%%%Count%
If %Skip%==0 (Set Skip=) Else (Set Skip=skip=%Skip%)
Set RandomFile=
For /F "%Skip% tokens=*" %%f In ('Dir /A-D /B *.txt') Do (If "!RandomFile!"=="" Set RandomFile=%%f)
Start "%RandomFile%"

Change *.txt in each For line to whatever file extension your movies use. You can write multiple extensions separated by spaces, for example *.mpg *.avi *.divx

The Start line will open the file in the default program. You may want to change this to run your movie player, such as:

"C:\Program Files\MPC-HC\MPC-HC.exe" "%RandomFile%"
share|improve this answer

This PowerShell script selects a random video from the script's folder and plays it

  1. Save the following code as somefilename.ps1 inside your movie folder. Note the extension .ps1

    $formats = @("*.avi","*.mp4","*.flv","*.mpg","*.wmv","*.mpeg","*.mov")
    $dir = Split-Path $MyInvocation.MyCommand.Path
    gci "$dir\*" -include $formats -recurse | Get-Random -Count 1 | Invoke-Item
  2. Create a shortcut to your powershell.exe. Default path on Windows 7 is

  3. Modify the target line of your shortcut and append this to execute the Powershell script

    -executionpolicy bypass -File "D:\your\video\path\somefilename.ps1"

What to adjust

  • Remove -recurse if you want to search only in your rootfolder and not in subfolders too
  • Expand the $formats list if you have exotic file container formats

Used commands: Split-Path, Get-ChildItem, Get-Random, Invoke-Item

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.