473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Select random files out of a directory

for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.

Jan 5 '07 #1
10 10568
Yes...
See opendir, readdir, rand
And get creative

Hendri Kurniawan
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
Jan 5 '07 #2

Hendri Kurniawan ¼g¹D¡G
Yes...
See opendir, readdir, rand
And get creative

Hendri Kurniawan
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
the problem is:

there are too many files in the folder, e.g. over 10K or more, i don't
want to list it

but i know that it must contains 10K files, so it is possible to get ,
say 1020th file under this folder?

Jan 5 '07 #3
>the problem is:
>there are too many files in the folder, e.g. over 10K or more, i don't
want to list it
but i know that it must contains 10K files, so it is possible to get ,
say 1020th file under this folder?
Use scandir() to return an array of filenames and array_rand() to pick a
random entry from that list.. (see the manual for details)

Chris
Jan 5 '07 #4
Unless other people knows any other methods.

for($i = 0; $i < 1024; $i++) $fileName = readdir($dirHandler);
print $fileName;

Now if you know the file names, that is another matter.

Hendri Kurniawan

howa wrote:
Hendri Kurniawan ¼g¹D¡G
>Yes...
See opendir, readdir, rand
And get creative

Hendri Kurniawan
howa wrote:
>>for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.

the problem is:

there are too many files in the folder, e.g. over 10K or more, i don't
want to list it

but i know that it must contains 10K files, so it is possible to get ,
say 1020th file under this folder?
Jan 5 '07 #5
BTW scandir is not available prior to PHP5
Just a note

Hendri Kurniawan

Skeleton Man wrote:
>the problem is:
there are too many files in the folder, e.g. over 10K or more, i don't
want to list it
but i know that it must contains 10K files, so it is possible to get ,
say 1020th file under this folder?

Use scandir() to return an array of filenames and array_rand() to pick a
random entry from that list.. (see the manual for details)

Chris

Jan 5 '07 #6
If you have access to system commands you could try the following:

1) Get the number of files using something like: ls | wc
2) Use your random generator
3) Now pick your line e.g. for file # 123: ls | head -123 | tail -1

Have fun
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
Jan 5 '07 #7
$dir = './';
$files = array();
$file = '';
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) $files[] = $file;
}
}
}

// specify an int for the optional 2nd arg if you
// need more than one random file
$randomKeys = array_rand($files);
$randomFile = $files[$randomKeys[0]];

$files holds an array of all files in the directory specified in the
$dir variable. This can take the place of scandir if you're running on
a version of PHP prior to 5.x.

On Jan 4, 9:30 pm, Hendri Kurniawan <ask...@email.comwrote:
BTW scandir is not available prior to PHP5
Just a note

Hendri Kurniawan

Skeleton Man wrote:
the problem is:
there are too many files in the folder, e.g. over 10K or more, i don't
want to list it
but i know that it must contains 10K files, so it is possible to get ,
say 1020th file under this folder?
Use scandir() to return an array of filenames and array_rand() to pick a
random entry from that list.. (see the manual for details)
Chris
Jan 5 '07 #8

Martin Mandl - m2m tech support ¼g¹D¡G
If you have access to system commands you could try the following:

1) Get the number of files using something like: ls | wc
2) Use your random generator
3) Now pick your line e.g. for file # 123: ls | head -123 | tail -1

Have fun
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
thanks....all examples should work for me...but a little bit slow as my
folder contains more than 10K files....thanks anyway

Jan 5 '07 #9
Another idea: Why don't you load the names of your files into a
database. If you do this with an automatic task, it does not matter how
long it takes. ... and getting a random result form the database should
not take that long ...

howa wrote:
thanks....all examples should work for me...but a little bit slow as my
folder contains more than 10K files....thanks anyway
Jan 5 '07 #10
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Curtis schrieb:
$dir = './';
$files = array();
$file = '';
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) $files[] = $file;
}
}
}

// specify an int for the optional 2nd arg if you
// need more than one random file
$randomKeys = array_rand($files);
$randomFile = $files[$randomKeys[0]];

Without the memory overhead selecting one random file could in addition
look like this then:

$dir = './';
$file = '';
mt_srand(time());
$probability=mt_getrandmax()/10000;
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) {
if (mt_rand()<$probability) break;
}
}
}
}

I know it's far from perfect and it's not equally distributed but might
save some execution time if you accidently select the second file ;) as
well as you don't have the memory overhead and it takes at most as long
as the original script. Important here is the probability variable,
which should be adjusted with an estimated file count of the desired folder.

Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (MingW32)

iD8DBQFFnnuRyeCLzp/JKjARAgImAJ0WdU9IjvrF9+vgYqRGQk3ewvjW/ACfcrAx
i7zjE6lnU58u6SsykMualQs=
=4QPJ
-----END PGP SIGNATURE-----
Jan 5 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kingofkolt | last post by:
I have a directory of images, called "random". In it are the following files: 1.gif 2.gif 3.gif 4.gif I use this script to choose a random image and display it:
6
by: Acacia | last post by:
How would you generate a random number in C++?
4
by: SoulSniper | last post by:
Hi, I have been stuck on this for a few days now and have given up trawling through pages and pages of google results.. I'm just putting the finishing touches to a small game I've written....
4
by: brett | last post by:
On my website I have a directory filled with possible files that a user can select to download. Rather than going to the directory itself to download, I'd like the user to be able to select a...
5
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times...
1
by: Sahus Pilwal | last post by:
Hi, I hope someone can help me with this. I'm new to .NET and in fact server side programming and have a small query I'm sure... I'm using the System.IO Namespace with a For - each and If then...
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
2
by: arvindfs | last post by:
Hi, I have about 10 files in a directory and need to select them in random order every time I run the program. I guess that this can be done using commands "Dir" and "Rnd". Can someone help me?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.