mIRC Homepage
Posted By: harl91 randomly pick a file - 27/04/07 10:05 PM
i need a script that will randomly pick a file name in a folder that is located in the same folder that mirc is in.

for example, if i have three files

a.map
b.map
c.mao

i want it to randomly pick a file and set it into a variable
Posted By: maroon Re: randomly pick a file - 27/04/07 10:44 PM
First you need to count how many files there are:

var %count = $findfile(c:\path\name\,*.*,0,1)

then pick a random number from 1 to %count:

var %count = $rand(1,%count)

then pick the random'th file:

var %count = $findfile(c:\path\name\,*.*,%count,1)

You can also make some changes to look at *.map instead of *.*, or remove the ,1 to make it look in subfolders beneath.
Posted By: billythekid Re: randomly pick a file - 27/04/07 11:49 PM
Code:
//echo $findfile(C:mirc/folder,*.map,$rand(1,$findfile(C:mirc/folder,*.map,0,0)),0)


btk
Posted By: qwerty Re: randomly pick a file - 28/04/07 10:08 AM
maroon already gave the answer. Your one-liner is based on the same idea except it's sloppier (0 for the [depth] parameter, forward slashes in paths, no slash after the drive letter; all these are non-standard, although tolerated).
Posted By: billythekid Re: randomly pick a file - 28/04/07 12:22 PM
how come a depth of 0 is wrong? I figured that if you want to go zero dirs deeper then 0 would be the correct number, 1 should check that folder and one deeper imo, although i didnt test this, i've always used 0 deep for that folder only.

as for the slashes i did notice that but couldn't be bothered changing it! lol

btk
Posted By: maroon Re: randomly pick a file - 28/04/07 02:15 PM
Originally Posted By: billythekid
how come a depth of 0 is wrong? I figured that if you want to go zero dirs deeper then 0 would be the correct number, 1 should check that folder and one deeper imo, although i didnt test this, i've always used 0 deep for that folder only.

snip


depth 0 is the same as dept 999999, the same way /dcc maxcps of 0 is same as 999999. Depth 1 does 1-level search, so it searches c:\path\name\ only, while dept 2 would also search in c:\path\name\subdir\ - so if you're wanting to only get files from the base folder, anything besides level-1 wastes time by searching for total-files in all subfolders.

If you're just searching for the 1st file, and there's at least 1 matching file in the base folder, then there is no difference whether or not you limit the depth or not.
Posted By: qwerty Re: randomly pick a file - 28/04/07 02:20 PM
Actually, anything <= 0 is treated as 1, not 999999. The latter is treated as it is, ie makes $findfile scan up to 999,999 directories deep.
Posted By: maroon Re: randomly pick a file - 28/04/07 02:32 PM
Sorry, you're right, it was the absence of the depth parameter that was treated as infinite depth. I did notice that $findfile($mircdir,*.mrc,0,%null_variable) returns nothing at all, and isn't the same as $findfile($mircdir,*.mrc,0). I wonder if it's supposed to be that way, though you can always sanitize the variable by changing $null into 0 or 999999.
Posted By: RusselB Re: randomly pick a file - 28/04/07 02:33 PM
I thought the same thing, but I tested it before going to post, and the test showed quite easily that I was thinking incorrectly.
If the depth parameter is omitted, then it does the search of the specified directory and any directories and sub-directories thereof.
Posted By: harl91 Re: randomly pick a file - 28/04/07 07:41 PM
maroons was fine, here it is

Code:
    set %bot.numberofmaps  $findfile(c:/winbolo/maps/,*.map,0,1)
    set %bot.mapnumber $rand(1,%bot.numberofmaps)
    set %bot.randmap1 $findfile(c:/winbolo/maps/,*.map,%bot.mapnumber,1)
    set %bot.randmap $right( %bot.randmap1 , -16 )
Posted By: The_JD Re: randomly pick a file - 06/05/07 05:02 AM
* JD laughs and thinks:

Code:
set %file $r(a,c) $+ .map


I write this because it could be his $mircdir or any other directory that contains non map files.
© mIRC Discussion Forums