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

something like grep

I'd like to open a directory and spider all the subdirectories and files.

What is the php term for doing this?

Seems like it should be here:

http://us3.php.net/manual/en/ref.filesystem.php

Perhaps I'm just having a bad day!

Jeff
Jul 29 '08 #1
5 1459
Hi,

You can use the opendir and readdir functions to traverse a directory
tree. There's an example in the doc at php.net/opendir.

If you want to do a text search within a directory tree, you can use
the egrep shell command with the -r (recursive) option on most
linuxes.

Regards,

John Peters

On Jul 29, 4:47 pm, Jeff <jeff@spam_me_not.comwrote:
I'd like to open a directory and spider all the subdirectories and files.

What is the php term for doing this?

Seems like it should be here:

http://us3.php.net/manual/en/ref.filesystem.php

Perhaps I'm just having a bad day!

Jeff
Jul 29 '08 #2
..oO(petersprc)
>You can use the opendir and readdir functions to traverse a directory
tree. There's an example in the doc at php.net/opendir.
Or use the various iterators of the SPL. They make it quite convenient
to loop through a directory and all that's beneath it if necessary.

Micha
Jul 29 '08 #3
Michael Fesser wrote:
.oO(petersprc)
>You can use the opendir and readdir functions to traverse a directory
tree. There's an example in the doc at php.net/opendir.

Or use the various iterators of the SPL. They make it quite convenient
to loop through a directory and all that's beneath it if necessary.
Having just now looked at this, I can see how this might work (It
seems to have some of the file system operators I'm familiar with). But
the documentation is poor and I'm not sure where I would even start.

Do you have any sample code for this?

Recursion and readdir is it's own can of worms, but at least I
understand it!

Jeff
>
Micha
Jul 29 '08 #4
..oO(Jeff)
>Michael Fesser wrote:
>.oO(petersprc)
>>You can use the opendir and readdir functions to traverse a directory
tree. There's an example in the doc at php.net/opendir.

Or use the various iterators of the SPL. They make it quite convenient
to loop through a directory and all that's beneath it if necessary.

Having just now looked at this, I can see how this might work (It
seems to have some of the file system operators I'm familiar with). But
the documentation is poor and I'm not sure where I would even start.

Do you have any sample code for this?
To conveniently loop through an entire directory tree $path you would
need two iterators, like this:

$path = '/foo/bar/whatever';
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
if ($item->isFile()) {
...
}
}

Of course this doesn't pay attention to the actual directory or nesting
level, it simply loops through _all_ found entries and sub entries.

See <http://www.php.net/~helly/php/ext/spl/classSplFileInfo.htmlfor
the various methods you can call on such file objects (just scroll down
a bit to "Public Member Functions").

Micha
Jul 30 '08 #5
Michael Fesser wrote:
.oO(Jeff)
>Michael Fesser wrote:
>>.oO(petersprc)

You can use the opendir and readdir functions to traverse a directory
tree. There's an example in the doc at php.net/opendir.
Or use the various iterators of the SPL. They make it quite convenient
to loop through a directory and all that's beneath it if necessary.
Having just now looked at this, I can see how this might work (It
seems to have some of the file system operators I'm familiar with). But
the documentation is poor and I'm not sure where I would even start.

Do you have any sample code for this?

To conveniently loop through an entire directory tree $path you would
need two iterators, like this:

$path = '/foo/bar/whatever';
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
if ($item->isFile()) {
...
}
}
Thanks,

I see that this gives an alphabetically sorted list.
/a.html
/a/a.html
/ab.html
/b.html

I find it fascinating that can be done, but not useful at the moment.

Can I look at the root first and then recurse through the directories?
Or is this easier with recursive readdir?

I imagine this is possible, but I haven't a clue how to go about that.
I take it this is documentation for something that has no other
documentation?

Jeff

>
Of course this doesn't pay attention to the actual directory or nesting
level, it simply loops through _all_ found entries and sub entries.

See <http://www.php.net/~helly/php/ext/spl/classSplFileInfo.htmlfor
the various methods you can call on such file objects (just scroll down
a bit to "Public Member Functions").

Micha
Jul 30 '08 #6

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

Similar topics

13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
2
by: John E. Jardine | last post by:
Hi, Problem: Executing 's///' has a side effect on grep null string matching. If line 62, the substitution, is executed the last two values returned by grep and printed on lines 68, 69 are...
1
by: Al Belden | last post by:
Hi all, I've been working on a problem that I thought might be of interest: I'm trying to replace some korn shell scripts that search source code files with perl scripts to gain certain features...
3
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
4
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a...
4
by: js | last post by:
Just my curiosity. Can python beats perl at speed of grep-like processing? $ wget http://www.gutenberg.org/files/7999/7999-h.zip $ unzip 7999-h.zip $ cd 7999-h $ cat *.htm bigfile $ du -h...
3
by: soniamadtha | last post by:
hello everyone, i have the following values : 98000345 and i would like to search this value in all of the sql files in the directory /temp/*sql normally on unix its grep( ' 98000345 '...
3
by: MyMarlboro | last post by:
print Dumper(@data); _____________ $VAR1= '2008,Car,Medium1,5000'; $VAR2= '2008,Car,Medium2,5000'; $VAR3= '2007,Plane1,Medium1,20000'; $VAR4= '2006,Boat,Small1,20000'; $VAR5=...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.