Jump to content

Traversing files and directories: Difference between revisions

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


'''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:

Revision as of 11:17, 12 July 2006

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
 {
   function visitFile($file)
   {
     $exploded = explode('/',$file);
     $filename = array_pop($exploded);
     $filepath = implode('/',$exploded).'/';
     
     if (substr($filename,0,strlen('localization_'))==='localization_' && substr($filename,-5)==='.conf')
     {
       echo $file.'<br />';
     }
   }
 }
 
 $traverser = &atknew('atk.utils.atkdirectorytraverser');
 $traverser =  new atkDirectoryTraverser();
 $traverser->addCallbackObject(new localefinder());
 $traverser->traverse(atkconfig('atktempdir'));