473,786 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10653
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($dirHan dler);
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($fil es);
$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.c omwrote:
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

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

Similar topics

4
3418
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
20707
by: Acacia | last post by:
How would you generate a random number in C++?
4
3802
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. The game uses the FMOD sound library to play an MP3 as background music. I got slightly bored of listening to the same song over and over and realised it would be much nicer if the game could pick an MP3 at random from a folder...
4
2057
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 single file from a select box embedded on an html page. I'm looking for a way that I can use javascript to do this for me. I know that perl could get these filenames for me easily, but is javascript able to put these into a select box once I have Perl...
5
2951
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 I try to start to run/debug a project (through Visual Studio) Server Error in '/Intranet' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during...
1
2666
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 statement to randomly generate images to display in a <asp:image/image control. The random images are selected from a fixed folder on the webserver. All works fine but only if the images in the folder are of image format say jpg or gif. However...
39
2471
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 MyClass from module2. module2 does not import random. module1 sets a seed like this::
2
1118
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? Thanks in advance.
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.