How do I open a random file in a folder, and set that only files with the specified filename extension(s) should be opened?

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.

galacticninja

Posted 2012-11-30T14:15:27.750

Reputation: 5 348

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 – 2012-11-30T14:50:11.757

@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 – 2012-11-30T14:55:09.400

Answers

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

Posted 2012-11-30T14:15:27.750

Reputation: 756

I know this is 2 years old, but how do I look for files also in subfolders? – rluks – 2014-09-13T15:32:30.770

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 – 2012-11-30T14:57:16.527

@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 – 2012-11-30T14:59:20.613

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 – 2012-11-30T15:06:50.907

@galacticninja Just prepend the glob-string with your path, like: files = glob.glob("/my/path/*.jpg") – poplitea – 2012-11-30T15:13:55.667

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 – 2012-11-30T15:19:29.127

@galacticninja You could assign a variable with mypath=raw_input("Enter path:"), and then use that in the glob... – poplitea – 2012-11-30T15:21:29.593

By the way, do you know if that script can handle files with Unicode characters in their filenames? (Unlike the batch script I mentioned in my question.) Also, I made a batch script that makes it easy to call the Python script in the folder I want to open random files of. Let me know what you think. I put it here as an answer: http://superuser.com/a/515482/10259

– galacticninja – 2012-12-06T06:07:05.003

I just tested this and found out that it doesn't support files with Unicode characters in their filenames (it won't open the file). Know any possible workarounds for this? – galacticninja – 2012-12-13T05:45:50.673

@galacticninja I don't know. If you have (further) programming questions related to this, I suppose you'll get better help at StackOverflow. – poplitea – 2012-12-13T13:09:15.143

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.

galacticninja

Posted 2012-11-30T14:15:27.750

Reputation: 5 348