Re: something like grep



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.html> for
the various methods you can call on such file objects (just scroll down
a bit to "Public Member Functions").

Micha
.



Relevant Pages

  • Re: something like grep
    ... Or use the various iterators of the SPL. ... To conveniently loop through an entire directory tree $path you would ... it simply loops through _all_ found entries and sub entries. ...
    (comp.lang.php)
  • Re: Are Python deques linked lists?
    ... they don't work well with Python iterators, ... for loop syntax in favor of Python's frowny-sad while loops. ... it is trivial to turn a frowny loop into ... as a node reference that I alleged rendered linked lists a bit ...
    (comp.lang.python)
  • Re: Ruby Curriculum for coworkers
    ... >> Have em start with HelloWorld, then a loop... ... non-motivated learner with iterators, he'll bail. ... > "Favor composition over inheritance." ...
    (comp.lang.ruby)
  • Re: Vector.Erase?
    ... only on iterators, not pointers. ... Don't you think setting i to zero would be a problem when the outer loop is ... gratuitously embed expressions with side-effects inside other expressions): ...
    (microsoft.public.vc.mfc)
  • Re: what do you think about my code
    ... the lack of documentation. ... If there's time pressure, then I would probably skip the ... collect (loop repeat 3 collect (read s))))) ... for i from 1 upto num-students ...
    (comp.lang.lisp)