Shoutbox

Random Selection - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Random Selection (/showthread.php?tid=71355)

Random Selection by mad_willsy on 02-03-2007 at 10:22 PM

How do i select a random file from a directory?


RE: Random Selection by Ezra on 02-04-2007 at 12:04 AM

put all the filenames in an array and choose the index with a random number.


RE: Random Selection by mad_willsy on 02-04-2007 at 11:26 AM

Can i have a snippet please - I only know php!

I need to...

1. Open the folder
2. Put all filenames into an array
3. Select a random file
4. Put it into a variable, say DPName.


RE: Random Selection by Felu on 02-04-2007 at 01:27 PM

code:
function RandomFile(Folder){//The Function
    FileArray = new Array();//The Array
    for(var e = new Enumerator(fso.GetFolder(Folder).Files); !e.atEnd(); e.moveNext())//The Enumerator
        FileArray.push(e.item());//Push the files into the array
    File = FileArray[Math.floor(Math.random()*FileArray.length)];//Getting a Random File
    return File;//Return the random File
}

RE: Random Selection by hmaster on 02-04-2007 at 01:54 PM

You forgot to declare the fso variable:

code:
var fso = new ActiveXObject("Scripting.FileSystemObject");

RE: Random Selection by Matti on 02-04-2007 at 05:04 PM

Also note that the returned object is a File object. Check MSDN for the available methods and properties of this kind of object. ;)


RE: Random Selection by mad_willsy on 02-05-2007 at 07:11 PM

Sweet, thanks.