2

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
 :sub1
 rem For each found file set counter + 1.
 set /a "counter+=1"
 goto :eof
 :: end of sub1

 :: start of sub2
 :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.

Purposes:

  • 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.

An Dorfer
  • 1,178

3 Answers3

1

If someone wants to open certain files (say only videos) from directory and all the sub-directories under it. This can be used

import random,os
import regex as re

pattern = '..mp4|..mkv' #files with certain extension matches = []

for path, subdirs, files in os.walk('.'): #'.' means current directory for name in files: if re.match(pattern, name): matches.append(os.path.join(path,name))

file = random.choice(matches) cmd = "rundll32 url.dll,FileProtocolHandler "" + file + """ os.system(cmd)

Last lines are from poplitea's answer

To run this, save the above code somewhere as random_vid.py in say directory '.../folder1' go to the folder from where you want to open the files (from the folder or is sub-directories) and run

python .../folder1/random_vid.py

or save the above line in a text file and save it as random_vid.bat. place the bat file in the folder you want and double click it.

0

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 + "\""
os.system(cmd)

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

files = glob.glob("*.mkv")
files.extend(glob.glob("*.mp4"))
files.extend(glob.glob("*.tp"))
poplitea
  • 861
  • 1
  • 9
  • 20
0

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.