Take the 2-minute tour ×

How do I open a random file in a folder, and set that only files with the specified filename extension(s) should be opened? (While preferably, supporting Unicode filenames too.)

I've already looked around and found this batch script (.BAT):

@echo off & setlocal
 :: start of main
 rem Set your path here:
 set "workDir=C:\DVDCOVERS"

 rem Read the %random%, two times is'nt a mistake! Why? Ask Bill.
 rem In fact at the first time %random% is nearly the same.
 @set /a "rdm=%random%"
 set /a "rdm=%random%"

 rem Push to your path.
 pushd "%workDir%"

 rem Count all files in your path. (dir with /b shows only the filenames)
 set /a "counter=0"
 for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1

 rem This function gives a value from 1 to upper bound of files
 set /a "rdNum=(%rdm%*%counter%/32767)+1"

 rem Start a random file
 set /a "counter=0"
 for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2

 rem Pop back from your path.
 popd "%workDir%"

 goto :eof
 :: end of main

 :: start of sub1
 rem For each found file set counter + 1.
 set /a "counter+=1"
 goto :eof
 :: end of sub1

 :: start of sub2
 rem 1st: count again,
 rem 2nd: if counted number equals random number then start the file.
 set /a "counter+=1"
 if %counter%==%rdNum% (start "" "%fileName%")
 goto :eof
 :: end of sub2

 :: -snap--- end of batch

Source: http://forums.majorgeeks.com/showthread.php?t=181574

It works in opening any random file in a folder, but I would like to be able to set that only files with the specified filename extension(s) should be opened. (e.g. A folder contains .MKV (video), .TP (video), .MP4 (video) and .JPG (image) files, and I would like to randomly open only video files, and not the .JPG image files.)

It also does not support Unicode filenames. It makes Windows output this error message if it randomly opens a file with a Unicode filename:

Windows cannot find (filename of file with Unicode filename, with the Unicode characters replaced with a question mark). Make sure you typed the name correctly, and try again.


  • If you would like to watch a random video from a folder, but the folder also contains non-video files
  • If you would like to view a random image from a folder, but the folder also contains non-image files.
  • Etc.

Suggestions to improve the .BAT file code (especially the 'randomness', as I often get the same file two-three times successively) or another better solution (even a non-batch script) is welcome. I am using Windows 7.

share|improve this question
Would you be open to writing this in something other than a batch-script? It would be a piece of cake to achieve with far less code in for example Python... –  poplitea Nov 30 '12 at 14:50
@poplitea Yes. I put the .BAT script in there to illustrate what I already found and to give people an idea of what I'm looking for. –  galacticninja Nov 30 '12 at 14:55
add comment

2 Answers

up vote 1 down vote accepted

In Python, you can open a random JPG-file like this:

import glob,random,os
files = glob.glob("*.jpg")
file = random.choice(files)
print "Opening file %s..." % file
cmd = "rundll32 url.dll,FileProtocolHandler \"" + file + "\""

To open video-files like .MKV, .MP4 and .TP, replace the line files = glob.glob("*.jpg") with these lines:

files = glob.glob("*.mkv")
share|improve this answer
If I would like to add another filename extension, aside from .JPG (e.g. a PNG file), how would I edit the code? EDIT: You have ninja-edited this info into your answer. =) Thanks. –  galacticninja Nov 30 '12 at 14:57
@galacticninja To choose only PNG-files, replace files = glob.glob("*.jpg") with files = glob.glob("*.png"). To choose among multiple filetypes, see my edit (the example with mkv, mp4, tp). –  poplitea Nov 30 '12 at 14:59
How do I specify which folder is selected? Is it similar to this one (a Python script solution to a similar SU question of mine)? –  galacticninja Nov 30 '12 at 15:06
@galacticninja Just prepend the glob-string with your path, like: files = glob.glob("/my/path/*.jpg") –  poplitea Nov 30 '12 at 15:13
What if I want to use an argument to enter the folder directory into the script? This is similar to the above solution I mentioned. This is so I could just use BAT files and just edit the BAT files for each specific folder I want to use the Python script on (rather than creating multiple .PY files). I'm not familar with Python coding, so I'm asking these questions. =) –  galacticninja Nov 30 '12 at 15:19
show 4 more comments

This is an addition to poplitea's answer.

To use the Python script in poplitea's answer, I saved the Python script at C:\Programs\Scripts\, with the filename, open-random-video.py (Python script that opens random videos).

I then saved the following script as a batch file (.BAT):

C:\Python27\python.exe "C:\Programs\Scripts\open-random-video.py" cd

Note that: C:\Python27\ is where Python is installed. This directory may change depending on where you installed Python. cd means the current directory.

I then put the .BAT file in the folders I want to open random files of, and just run the .BAT file if I want a random file opened in that folder.

share|improve this answer
add comment

Your Answer


