Jump to content

Traversing files and directories: Difference between revisions

From NusaATK
No edit summary
despam
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Howto|Easy|[[Boy Baukema]] <boy@achievo.org>}}
The atkDirectoryTraverser can be used to recursively traverse a directory structure.<br />
The atkDirectoryTraverser can be used to recursively traverse a directory structure.<br />
For each directory or file encountered, it calls callback methods on one or more callbackobjects.<br />
For each directory or file encountered, it calls callback methods on one or more callbackobjects.<br />
Line 6: Line 8:
'''Example''' where directory traverser is used to find all localization files in the temporary directory:
'''Example''' where directory traverser is used to find all localization files in the temporary directory:


<syntaxhighlight lang="php">
   class localefinder
   class localefinder
   {
   {
    var $m_locales = array();
   
     function visitFile($file)
     function visitFile($file)
     {
     {
Line 16: Line 21:
       if (substr($filename,0,strlen('localization_'))==='localization_' && substr($filename,-5)==='.conf')
       if (substr($filename,0,strlen('localization_'))==='localization_' && substr($filename,-5)==='.conf')
       {
       {
         echo $file.'&lt;br /&gt;';
         $this->m_locales[] = $file;
       }
       }
    }
   
    function getLocales()
    {
      return $this->m_locales;
     }
     }
   }
   }
    
 
   // Initialise objects
   $traverser = &atknew('atk.utils.atkdirectorytraverser');
   $traverser = &atknew('atk.utils.atkdirectorytraverser');
   $traverser = new atkDirectoryTraverser();
   $localefinder = new localefinder();
   $traverser->addCallbackObject(new localefinder());
   $traverser->addCallbackObject($localefinder);
  // Traverse the temporary directory
   $traverser->traverse(atkconfig('atktempdir'));
   $traverser->traverse(atkconfig('atktempdir'));
  // Get all the found files
  $locales = $localefinder->getLocales();
</syntaxhighlight>

Latest revision as of 06:12, 9 July 2007

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_' && substr($filename,-5)==='.conf')
      {
        $this->m_locales[] = $file;
      }
    }
    
    function getLocales()
    {
      return $this->m_locales;
    }
  }

  // Initialise objects
  $traverser = &atknew('atk.utils.atkdirectorytraverser');
  $localefinder = new localefinder();
  $traverser->addCallbackObject($localefinder);
  // Traverse the temporary directory
  $traverser->traverse(atkconfig('atktempdir'));
  // Get all the found files
  $locales = $localefinder->getLocales();