Using file tally to seed random # generator

August 29, 2010 at 01:28:41
Specs: Windows 7, Phenom II x4 945
Please forgive me for reposting this content, but the subject line I provided gives no clue to what I actually wanted.

What I'm trying to do is write a batch file that will randomly select a wav file and pass the name to a program called playwav.exe to play. I'll drop a shortcut to the batch file into my Startup folder. I searched this site and found one solution by Mechanix2go that works for me with only very minor modifications (http://www.computing.net/answers/programming/batch-select-random-file/15002.html). So far, here is my solution:

=============================================

@echo off

setlocal enabledelayedexpansion

set /a rand=%random%%%37+1

D:
CD D:\Users\IDigWeeds\Sounds\Startup\"

set nbr=1
for /f "delims=*" %%1 in ('dir /a-d /b *.wav') do (
set file=%%1
set /a nbr+=1
if !nbr! gtr !rand! goto :getout
)

:getout
xcopy /y "%file%" D:\Users\IDigWeeds\Sounds\StartUp\MyStartUp.wav

D:
CD "D:\Users\IDigWeeds\Settings\PlayWav\"
PlayWav.exe D:\Users\IDigWeeds\Sounds\StartUp\MyStartUp.wav

=========================================

One thing stilll bothers me... my collection is ever expanding, and editing the random variable to fit my colllection is not satisfactory. How can I modify this file to make it count up the number of files in the directory and use that to seed my random variable?

IDigWeeds


See More: Using file tally to seed random # generator

Report •

#1
August 29, 2010 at 03:37:42
I've done some butchery on your script, it's totally untested. Hope it helps.

@echo off

setlocal enabledelayedexpansion

:: Set default directory.
pushd D:\Users\IDigWeeds\Sounds\Startup\

:: Extract the number of .wav files in the default directory and
:: generate the random number.
for /f "tokens=1-2*" %%1 in ('dir *.wav ^| find /n "File(s)"') do (
    set files=%%2
    set /a files=!files!-1
    set /a rand=(%random%%%%files%)+1
)

set nbr=1
for /f "delims=*" %%1 in ('dir /a-d /b *.wav') do (
set file=%%1
set /a nbr+=1
if !nbr! gtr !rand! goto :getout
)

:getout
xcopy /y "%file%" MyStartUp.wav
popd

pushd D:\Users\IDigWeeds\Settings\PlayWav\
PlayWav.exe D:\Users\IDigWeeds\Sounds\StartUp\MyStartUp.wav
popd


Report •

#2
August 29, 2010 at 10:53:44
Thanks Wahine!. I'll give this a try and see how it goes!

By the way, it's been years since I have done any batch programmming, what little I have done... what do pushd and popd do, and for that matter, what's this delayed expansion thing do as well?


Report •

#3
August 29, 2010 at 11:26:23
Welll that didn't go so well. I actually tried what you did before you suggested it. I saw a sample of a how to count up files in a directory elsewhere in the forum. I fed the result to a variable and then plugged it into the random number generator and had no success at it. I think it might have something to do with the syntax of our solution, but can't be sure. Your current code I cannot troubleshoot because as soon as it is executed, the window opens and immediatly closes. I added several pauses and turned echo on, but same result. Any other suggestions?

Report •

Related Solutions

#4
August 29, 2010 at 14:32:30
Sorry about handing you a few problems.

Enabledelayedexpansion - I probably overuse this option, most of the time it's not required. An explanation of what it does is here: http://ss64.com/nt/setlocal.html

Pushd and Popd - Pushd is used to change the default directory, Popd resets the default directory to whatever it was before the Pushd command was used. They are included in the list here: http://ss64.com/nt/

I will set up a trial here to execute my script and get back to you soonest. Unfortunately this could take some time.


Report •

#5
August 29, 2010 at 14:39:23
Ok. I have a solution, thanks again to Wahine and another previous post by mechanix2go:

========================================

@echo off

setlocal enabledelayedexpansion

pushd D:\Users\IDigWeeds\Sounds\Startup\"

set files=
for /f "tokens=* delims= " %%a in ('dir/b *.wav') do (
set /a files+=1
)

set /a rand=(%random%%%%files%)+1

set number=1
for /f "delims=*" %%1 in ('dir /a-d /b *.wav') do (
set file=%%1
set /a number+=1
if !number! gtr !rand! goto :getout
)

:getout

xcopy /y "%file%" MyStartUp.wav
popd

pushd D:\Users\IDigWeeds\Settings\PlayWav\
PlayWav.exe D:\Users\IDigWeeds\Sounds\StartUp\MyStartUp.wav
popd

=======================================

By the way.. don't anyone ask me how any of this works .. I haven't a clue!

Idigweeds


Report •

#6
August 29, 2010 at 21:05:55
Magical IDigWeeds. For what it's worth here is my finalized script tested and works on my system.

Good luck

@echo off
cls
setlocal enabledelayedexpansion

pushd D:\users\idigweeds\sounds\startup\

for /f "tokens=1-2*" %%1 in ('dir *.wav ^| find /n "File(s)"') do (
    set files=%%2
    set /a files=!files!-1
    
)
set /a rand=(%random% %%!files!)+1

set nbr=1
for /f "delims=*" %%1 in ('dir /a-d /b *.wav') do (
set file=%%1
set /a nbr+=1
if !nbr! gtr !rand! goto :getout
)

:getout
d:\users\idigweeds\settings\playwav\PlayWav.exe "%file%" > nul
popd


Report •

#7
August 29, 2010 at 21:59:25
Your version sems to work just as well on my system as yours... and your script is a bit cleaner, too. I may opt to use yours instead!

Report •

#8
August 30, 2010 at 01:15:58
A win at last.

Good luck & thanks for coming back to report your success.


Report •

#9
August 30, 2010 at 14:12:21
@echo off & setLocal EnableDELAYedeXpansion

pushd D:\users\idigweeds\sounds\startup\
set T=

for /f "tokens=* delims= " %%a in ('dir/b/a-d *.wav') do (
set /a T+=1
)
set /a R=!random!%%T%+1
set N=
for /f "tokens=* delims= " %%a in ('dir/b/a-d *.wav') do (
set /a N+=1
if !N! equ !R! (
  echo.copy %%a blabla
  goto :eof
)
)
:: and so on...


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question