473,228 Members | 1,594 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,228 software developers and data experts.

Faster read of filenames

Hi all

I use the opendir and readdir to search for files... but they read in
all files, so sometimes I get several hundreds.
If there is a way to give a wildcard, it might be a lot faster.
Since I do this search 100+ times at certain times, this is the thing
which slows me down.
Disableing this part speeds my code up by 90%.

Any ideas?

Sonnich
if($handle = opendir($sSearchPath))
{
while (false !== ($file = readdir($handle)))
{
$file2 = strtoupper($file);
if( (strpos($file2, $code)!==False) && (strpos($file2,
".PDF")!==False))
{
$files[] = $file;
$paths[] = $sSearchPath;
}
}
closedir($handle);
}

Oct 31 '06 #1
5 1308
On 31 Oct 2006 03:22:03 -0800, in comp.lang.php "Sonnich"
<so************@elektrobit.com>
<11**********************@e3g2000cwe.googlegroups. comwrote:
>| Hi all
|
| I use the opendir and readdir to search for files... but they read in
| all files, so sometimes I get several hundreds.
| If there is a way to give a wildcard, it might be a lot faster.
| Since I do this search 100+ times at certain times, this is the thing
| which slows me down.
| Disableing this part speeds my code up by 90%.
|
| Any ideas?
|
| Sonnich
|
|
| if($handle = opendir($sSearchPath))
| {
| while (false !== ($file = readdir($handle)))
| {
| $file2 = strtoupper($file);
| if( (strpos($file2, $code)!==False) && (strpos($file2,
| ".PDF")!==False))
| {
| $files[] = $file;
| $paths[] = $sSearchPath;
| }
| }
| closedir($handle);
| }
glob() maybe?
foreach (glob("*.txt") as $filename)
{
echo "$filename size " . filesize($filename) . "\n";
}
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Oct 31 '06 #2
Sonnich wrote:
I use the opendir and readdir to search for files... but they read in
all files, so sometimes I get several hundreds.
If there is a way to give a wildcard, it might be a lot faster.
Since I do this search 100+ times at certain times, this is the thing
which slows me down.
Disableing this part speeds my code up by 90%.

Any ideas?
glob()? http://php.net/glob

If your files do not change very often, it may be better to have a
file (or database) with the current files and use that instead of
searching in the file system.

<?php
$filelist = file($path . '/filelist.txt');
foreach ($filelist as $fname) {
$files[] = trim($fname);
$paths[] = $path;
}
?>

Of course you'd have to keep filelist.txt updated.

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 31 '06 #3

Jeff North wrote:
On 31 Oct 2006 03:22:03 -0800, in comp.lang.php "Sonnich"
<so************@elektrobit.com>
<11**********************@e3g2000cwe.googlegroups. comwrote:
| Hi all
|
| I use the opendir and readdir to search for files... but they read in
| all files, so sometimes I get several hundreds.
| If there is a way to give a wildcard, it might be a lot faster.
| Since I do this search 100+ times at certain times, this is the thing
| which slows me down.
| Disableing this part speeds my code up by 90%.
|
| Any ideas?
|
| Sonnich
|
|
| if($handle = opendir($sSearchPath))
| {
| while (false !== ($file = readdir($handle)))
| {
| $file2 = strtoupper($file);
| if( (strpos($file2, $code)!==False) && (strpos($file2,
| ".PDF")!==False))
| {
| $files[] = $file;
| $paths[] = $sSearchPath;
| }
| }
| closedir($handle);
| }

glob() maybe?
foreach (glob("*.txt") as $filename)
{
echo "$filename size " . filesize($filename) . "\n";
}

That might do it, but it is case sensitive. And when trying something
like:

foreach (glob($search, GLOB_BRACE ) as $filename)
{
echo "$filename <br>";
}

I get absolutlely nothing. My search paths are something like:
\\server\somewhere\AB123*.pdf or \\server\somewhere\AB123*.PDF
Both returns a number of files when runned alone, but adding them
togther
$sx = "{".$s1.",".$s2."}";
does not do the trick.

BR
Sonnich

Oct 31 '06 #4
NC
Sonnich wrote:
Jeff North wrote:

glob() maybe?

That might do it, but it is case sensitive.
But of course it is. On Unix, file names are case-sensitive...
And when trying something like:

foreach (glob($search, GLOB_BRACE ) as $filename)
{
echo "$filename <br>";
}

I get absolutlely nothing. My search paths are something like:
\\server\somewhere\AB123*.pdf or \\server\somewhere\AB123*.PDF
Both returns a number of files when runned alone, but adding them
togther
$sx = "{".$s1.",".$s2."}";
does not do the trick.
Try one or more of these:

\\server\somewhere\*123*.pdf
\\server\somewhere\??123*.pdf
\\server\somewhere\{a,A}{b,B}123*.pdf

Cheers,
NC

Oct 31 '06 #5
After some heavy testing I found that opendir/readdir is app twice as
fast as glob, just as mentioned on www.php.net.
So my advice would be to use opendir/readdir in stead of glob.

BR
Sonnich

Oct 31 '06 #6

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

Similar topics

3
by: Ralph Freshour | last post by:
I have a PHP web app using MySQL - when I save a .jpg file named test's.jpg I see that the filename on the unix server is: test\'s.jpg - the filename I end up saving in my SQL table is named...
9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
3
by: andrea.gavana | last post by:
Hello NG, in my application, I use os.walk() to walk on a BIG directory. I need to retrieve the files, in each sub-directory, that are owned by a particular user. Noting that I am on Windows...
2
by: rbutch | last post by:
guys, i need a little help with this. this is working (well sort of) i get the info, but it's not moving to a new line as it iterates thru the array and all of the fields are like ONE HUGE LONG...
0
by: n33470 | last post by:
We have a web site that is being converted from the 1.1 format into 2.0. I've noticed that after the web project has been converted, the first time that the solution is opened in VS, all of the...
22
by: rtilley | last post by:
# Spaces are present before and after the XXX filename = ' XXX ' new_filename = filename.strip() if new_filename != filename: print filename Macs allow these spaces in file and folder...
0
by: ColdCoffee | last post by:
Scenario: My computer: name : PrasComp pass : Pras123 Domain : XYZ Computer I wanna access: Name : AnoComp pass : #Pras
0
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
3
by: Torsten Bronger | last post by:
Hallöchen! I'd like to map general unicode strings to safe filename. I tried punycode but it is case-sensitive, which Windows is not. Thus, "Hallo" and "hallo" are mapped to "Hallo-" and...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.