Saturday, January 28, 2017

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


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.


Answer



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"))

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...