How do I select a random file in a folder?
11,919
How do I select a random file in a folder?
Use the following batch file:
@echo off
setlocal
setlocal EnableDelayedExpansion
rem store the matching file names in list
dir /b *.txt /s 2> nul > files
rem count the match files
type files | find "" /v /c > tmp & set /p _count=<tmp
rem get a random number between 0 and count-1
set /a _random=%random%*(%_count%)/32768
rem we can't skip 0 lines
if %_random% equ 0 (
for /f "tokens=*" %%i in ('type files') do (
set _randomfile=%%i
echo !_randomfile!
goto :eof
)
) else (
for /f "tokens=* skip=%_random%" %%i in ('type files') do (
set _randomfile=%%i
echo !_randomfile!
goto :eof
)
)
The environment variable !_randomfile!
will contain the filename of a random file.
Notes:
- Remove
/s
if you don't want to match files in subfolders. -
0
=<%RANDOM%
<32767
so it won't work if you have more than32766
matching files.
Further reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- find - Search for a text string in a file & display all the lines where it is found.
- for /f - Loop command against the results of another command.
-
random - The Windows CMD shell contains a dynamic variable called
%RANDOM%
that can be used to generate random numbers.
Related videos on Youtube

Comments
-
ZEE 8 months
I am trying to select a random file of a specific type like *.mp4 from a folder (and optionally subfolders) using the Windows command line batch script (no PowerShell)
The file full path should be stored in a environment variable for further use
How can I achieve this?
-
ZEE almost 8 yearsHi david... good answer... but I think we can reduce that code (sorry I am an optimizer by nature) and create some error-prevention mecanism for the file number (file count before select)... THX
-
DavidPostill almost 8 years@ZEE I'm not sure what you with "file count before select" and "error prevention"? Can you explain a bit more?
-
ZEE almost 8 yearsIf we count the files,,, then we can assure a random number will always be in the considered range! Error prevent... no file selected outside the given range... detect if selected file exists and is readable... etc. ;-)
-
DavidPostill almost 8 years@ZEE Please read the answer again especially the comments (
rem
statements). First I count the files. Then I get a random number that is the correct range. The files must exist or elsedir
wouldn't have found them to be counted, The only thing not checked is that the file is readable. -
DavidPostill almost 8 years@ZEE How do you suggest checking if an mp4 file is readable?
-
ZEE almost 8 yearsHi David... I would suggest something like ( type file.ext >nul & if ERRORLEVEL 1 echo "FILE NOT ACCESIBLE" )... as you surely know the type is not the best option... if you are using GNU tools or CYGWIN in your windows just use ( tail -c 1 file.ext & ... ) instead of type... this way you only try to read 1 byte from the file...
-
DavidPostill almost 8 years@ZEE The answer was written for you, feel free to add whatever checking you want to your batch file ;) If you really want to be clever use a program to read the file and see if it containls a valid mp4 header ... you will also need to work out how to choose another file if there is an error. That will be a nice learning exercise for you!