473,372 Members | 856 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,372 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 10574
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 gDG
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 gDG
>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 gDG
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.