random file from folder


T

Thomas Karer

Hello!

I have the following problem.

There is a folder with mp3 files inside it.

I want to randomly pick a file an play this.

It would be great if there would be a way to get a random filename in a
variable.

thank you
tom
 
Ad

Advertisements

M

Matt Williamson

I have the following problem.
There is a folder with mp3 files inside it.

I want to randomly pick a file an play this.

It would be great if there would be a way to get a random filename in a
variable.
Here is one way

@echo off
cd /d %~dp0
setlocal enabledelayedexpansion
if exist mp3s.txt del mp3s.txt
set count=0
for /f "tokens=*" %%a in ('dir/s/b *.mp3') do (
set /a count+=1
echo !count! %%a>>mp3s.txt
)
@echo on
set total=!count!
set /a ST=%RANDOM%%%%total%
for /f "tokens=1,2*" %%g in ('findstr /B /L "%ST%" mp3s.txt') do (
echo yourmediaplayer.exe "%%h %%i"
)

HTH

Matt
 
M

Matt Williamson

There is a folder with mp3 files inside it.
Here, this is better. I found a little bug where if the number that is
generated occurs in the output file, it would return multiple lines. I also
made it so if you have already generated your list, you don't have to regen
it every time you want a random mp3, you can run it with a -C switch to
generate the list if you need to. If, for any reason, your list of mp3's
contains the string "~~$$~~" change that to something else, it can be
anything, just so long as it ISN'T in your file.

@echo off
cd /d %~dp0
setlocal enabledelayedexpansion

if /i "%~1"=="-C" (
if exist mp3s.txt del mp3s.txt
set count=0
for /f "tokens=*" %%a in ('dir/s/b *.mp3') do (
set /a count+=1
echo !count! %%a>>mp3s.txt
)
)

for /f "tokens=2 delims=:" %%a in ('find /C /V "~~$$~~" mp3s.txt') do (set
total=%%a)
set total=%total: =%
set /a ST=%RANDOM%%%%total%
for /f "tokens=1,2*" %%g in ('findstr /B /L "%ST%" mp3s.txt') do (
if "%%g" EQU "%ST%" (
echo %%h %%i
)
)
 
B

billious

Thomas Karer said:
Hello!

I have the following problem.

There is a folder with mp3 files inside it.

I want to randomly pick a file an play this.

It would be great if there would be a way to get a random filename in a
variable.

thank you
tom
Since building a full list of files in the entire drive would take a while,
I've restricted the following scans to
C:\documents and settings\ as a local root. Adjust to suit...


----- batch begins -------
[1]@echo off
[2]setlocal
[3]:: create a tempfile of .mp3s, prefixed by [sequencenumber]
[4]dir /s/b/a-d "C:\documents and settings\*.mp3" | find /n /v ""
"%temp%\mp3list.1"
[5]:: find the Count of Files
[6]for /f "usebackq delims=[]" %%i in ("%temp%\mp3list.1") do set ycf=%%i
[7]:: now select at random from list
[8]set /a ysn=(%random% %% %ycf%) + 1
[9]for /f "usebackq tokens=1*delims=[]" %%i in ("%temp%\mp3list.1") do if
%%i==%ysn% set yts=%%j
[10]:: clean up tempfile
[11]del "%temp%\mp3list.1"
[12]endlocal&set music=%yts%
[13]echo track selected : %music%
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be removed

%temp% is a temporary directory. The environment variable %temp% is
set to "C:\Documents and Settings\username\temp" by default. Since
this contains spaces, it would probably be better to enclose any
such filename in quotes, thus: "%temp%\...." Experienced batchers
will change their %temp% to something more sensible, like C:\temp.
The issue is that it's a temporary file. Replace the pathname with one
that suits you, remembering that any tempfiles created may overwrite
existing files.

Note line [12] here. It uses the parsing characteristic of CMD.EXE to set a
variable to the value of a variable in the LOCAL context.

By extension,

----- batch begins -------
[1]@echo off
[2]setlocal
[3]del select.seq 2>nul
[4]:: create a tempfile of .mp3s, prefixed by [sequencenumber]
[5]dir /s/b/a-d "C:\documents and settings\*.mp3" | find /n /v ""
"%temp%\mp3list.1"
[6]:: find the Count of Files
[7]for /f "usebackq delims=[]" %%i in ("%temp%\mp3list.1") do set ycf=%%i
[8]:: now select at random from list
[9]:selloop
[10]set /a ysn=(%random% %% %ycf%) + 1
[11]for /f "usebackq tokens=1*delims=[]" %%i in ("%temp%\mp3list.1") do if
%%i==%ysn% (echo %%j>>select.seq) else (echo [%%i]%%j>>"%temp%\mp3list.2")
[12]:: clean up tempfile
[13]del "%temp%\mp3list.1"
[14]if exist "%temp%\mp3list.2" ren "%temp%\mp3list.2" mp3list.1&goto
selloop
[15]endlocal
[16]echo tracks selected :
[17]type select.seq
[18]
------ batch ends --------

will build a random list of all of the mp3s in "select.seq"
 
F

foxidrive

Hello!

I have the following problem.

There is a folder with mp3 files inside it.

I want to randomly pick a file an play this.

It would be great if there would be a way to get a random filename in a
variable.

thank you
tom
I wrote this some time back which creates a playlist (batch file) of
your MP3 music in randomised order and uses a command line player to
play the songs.


@echo off
:: Plays MP3 music randomly and pauses for 420 seconds between songs
setlocal enabledelayedexpansion
set mp3folder=C:\NOCOPY\MISC\Music\
type nul>"%temp%.\temp1.txt"
for /f "delims=" %%a in ('dir "%mp3folder%*.mp3" /a:-d /b /s') do (
echo !random! ? %%a>>"%temp%.\temp1.txt"
)
sort <"%temp%.\temp1.txt" >"%temp%.\temp2.txt"
echo.@echo off>"%temp%.\randplay.bat"
type nul>"%temp%.\temp3.txt"
for /f "delims=" %%a in (%temp%.\temp2.txt) do (
set var=%%a
set var=!var:*? =!rem vbwav is freeware
rem control-break to abort the current songdel "%temp%.\temp?.txt"
call "%temp%.\randplay.bat"
del "%temp%.\randplay.bat"
 
B

billious

foxidrive said:
I wrote this some time back which creates a playlist (batch file) of
your MP3 music in randomised order and uses a command line player to
play the songs.


@echo off
:: Plays MP3 music randomly and pauses for 420 seconds between songs
setlocal enabledelayedexpansion
set mp3folder=C:\NOCOPY\MISC\Music\
type nul>"%temp%.\temp1.txt"
for /f "delims=" %%a in ('dir "%mp3folder%*.mp3" /a:-d /b /s') do (
echo !random! ? %%a>>"%temp%.\temp1.txt"
)
sort <"%temp%.\temp1.txt" >"%temp%.\temp2.txt"
echo.@echo off>"%temp%.\randplay.bat"
type nul>"%temp%.\temp3.txt"
for /f "delims=" %%a in (%temp%.\temp2.txt) do (
set var=%%a
set var=!var:*? =!
rem vbwav is freeware
rem control-break to abort the current song
del "%temp%.\temp?.txt"
call "%temp%.\randplay.bat"
del "%temp%.\randplay.bat"
Good idea.

How about


----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]for /f "delims=" %%i in ( ' dir /s/b "C:\documents and settings\*.mp3" ')
do echo !random!]%%i>>"%temp%\rnseq.txt"
[4]for /f "tokens=1*delims=]" %%i in ( ' sort ^<"%temp%\rnseq.txt" ') do
echo %%j
[5]del "%temp%\rnseq.txt"
[6]goto :eof
------ batch ends --------

where the "Echo %%j" on [4] could be
echo %%j>>filename
to send a random sequence to a file or
set varname=%%j
to set a varname to a candidate (the final one in rnseq.txt)
 
Ad

Advertisements

T

Thomas Karer

many thanks to all your really fine working solutions!

cu
tom
 
Ad

Advertisements

Joined
May 16, 2013
Messages
7
Reaction score
0
(answer, found at stackoverflow.com/a/20361034/2165415)
here is a CMD code, which outputs the random file name (customize it to your needs):
HTML:
@echo off & setlocal
 set "workDir=C:\source\folder"

 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% (
 :: OUTPUT ALERT BOX with FILENAME
 MSG * "%fileName%"
 )
 goto :eof
 :: end of sub2
 :: -snap--- end of batch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top