Jump to content

Traversing files and directories

From NusaATK
Revision as of 17:45, 5 July 2007 by Xs4Di7 (talk | contribs)

ATK Howto: Traversing files and directories

Complexity: Easy
Author: Boy Baukema <boy@achievo.org>

List of other Howto's

The atkDirectoryTraverser can be used to recursively traverse a directory structure.
For each directory or file encountered, it calls callback methods on one or more callbackobjects.
This is similar to the unix 'find' command with an execute option.
You pass a callbackobject that performs the actual action. The full pathname of the file is passed as a parameter to the callback method.

Example where directory traverser is used to find all localization files in the temporary directory:

  class localefinder
  {
    var $m_locales = array();
    
    function visitFile($file)
    {
      $exploded = explode('/',$file);
      $filename = array_pop($exploded);
      $filepath = implode('/',$exploded).'/';
      
      if (substr($filename,0,strlen('localization_'))==='localization_'