How do I open a random file in a folder, and set that only files with the specified filename extension(s) should be opened?
Solution 1
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"))
Solution 2
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.
Related videos on Youtube
Comments
-
galacticninja over 1 year
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.
-
poplitea over 11 yearsWould 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...
-
galacticninja over 11 years@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 over 11 yearsIf 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.
-
poplitea over 11 years@galacticninja To choose only PNG-files, replace
files = glob.glob("*.jpg")
withfiles = glob.glob("*.png")
. To choose among multiple filetypes, see my edit (the example with mkv, mp4, tp). -
galacticninja over 11 yearsHow do I specify which folder is selected? Is it similar to this one (a Python script solution to a similar SU question of mine)?
-
poplitea over 11 years@galacticninja Just prepend the glob-string with your path, like:
files = glob.glob("/my/path/*.jpg")
-
galacticninja over 11 yearsWhat 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 over 11 yearsBy 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: superuser.com/a/515482/10259
-
galacticninja over 11 yearsI 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?
-
poplitea over 11 years@galacticninja I don't know. If you have (further) programming questions related to this, I suppose you'll get better help at StackOverflow.
-
rluks over 9 yearsI know this is 2 years old, but how do I look for files also in subfolders?