Iterating a directory

The File System interface provides two classes for iterating through a directory:

TDirectoryIterator iterates through each file and directory name within a given directory. TDirectoryIterator does not iterate through subdirectories.

TDirectoryTreeIterator performs recursive iterations; it moves down through subdirectories as it encounters them. TDirectoryTreeIterator moves up only after iterating through all files and directories within a subdirectory.

Because directories can contain other directories as well as files, the results of iteration are often heterogenous.

TParentlessDirectoriesIterator iterates over all parentless (topmost or "root") directories on all mounted volumes and returns a TDirectory for the top of each separate directory hierarchy on the system.

Using TDirectoryIterator

Example::DisplayDirectory uses TDirectoryIterator to display the contents of the directory passed into this function. This uses three user-defined member functions (Example::PrintText, Example::PrintName, Example::PrintTime) which are not displayed here.

      void Example::DisplayDirectory (const TDirectory& theDirectory) const
      {
          TPropertyIDSet                      idSet;
          TPropertySet                        propertySet;
          TPropertyIDFor<TFileSystemEntityName>nameProp(TFileSystemEntity::kName);
          TPropertyIDFor<TTime>  createTimeProp(TFileSystemEntity::kCreationTime);
          TPropertyIDFor<TTime>modifyTimeProp(TFileSystemEntity::kModificationTime);
          TStandardText name;
          TTime date;
      
          // Set up the idSet to retrieve to the file system entity name, 
          // creation time, and modification time
      
          idSet.Add(nameProp);
          idSet.Add(createTimeProp);
          idSet.Add(modifyTimeProp);
      
          PrintText("Displaying contents of directory");
          PrintText(theDirectory.GetName());
          PrintText("...\n\n\n");
      
          // Create an iterator, passing in the directory to iterate, 
          // the idSet, and the no query identifier
      
          TDirectoryIterator iterator(theDirectory, idSet, TDirectoryIterator::kNoQuery);
      
          // Get the first entity in this directory (could be a file or another directory)
      
          TFileSystemEntity anEntity = iterator.First(propertySet);
      
          // keep iterating as long as there are entities left to iterate through
      
          while (anEntity != TDirectoryIterator::kEndOfIteration)
          {
              propertySet.Get(nameProp);
              name = nameProp.GetValue();
              PrintName(name, " ", "\n");
      
              propertySet.Get(createTimeProp);
              date = createTimeProp.GetValue();
              PrintName(date, " ", "\n");
      
              propertySet.Get(modifyTimeProp);
              date = modifyTimeProp.GetValue();
              PrintName(date, " ", "\n");
                  
              anEntity = iterator.Next(propertySet);
          }
      }

Using TDirectoryTreeIterator

Example::DisplayDirectoryTree is similar to Example::DisplayDirectory, but because it uses a TDirectoryTreeIterator instance, it returns the full tree below this directory--in addition to the directory contents.

      void Example::DisplayDirectory (const TDirectory& theDirectory) const
      {
          TPropertyIDSet                      idSet;
          TPropertySet                        propertySet;
          TPropertyIDFor<TFileSystemEntityName>nameProp(TFileSystemEntity::kName);
          TPropertyIDFor<TTime>  createTimeProp(TFileSystemEntity::kCreationTime);
          TPropertyIDFor<TTime>modifyTimeProp(TFileSystemEntity::kModificationTime);
          TStandardText                       name;
          TTime                               date;
      
          // Set up the idSet to retrieve to the file system entity name, 
          // creation time, and modification time
      
          idSet.Add(nameProp);
          idSet.Add(createTimeProp);
          idSet.Add(modifyTimeProp);
      
          // Create an iterator, passing in the directory to iterate, 
          // the idSet, and indicate that no query should be applied
      
          TDirectoryTreeIterator iterator(theDirectory, idSet, TDirectoryIterator::kNoQuery);
      
          // Get the first entity in this directory (could be a file or another directory)
      
          TFileSystemEntity anEntity = iterator.First(propertySet);
      
          // keep iterating as long as there are entities left to iterate through
      
          while (anEntity != TDirectoryTreeIterator::kEndOfIteration)
          {
              // Retrieve each property and print it...
      
              propertySet.Get(nameProp);
              name = nameProperty.GetValue();
              PrintName(name, "", "   ");
      
              propertySet.Get(createTimeProp);
              date = createTimeProp.GetValue();
              PrintDate(date, "", "   ");
      
              propertySet.Get(modifyTimeProp);
              date = modifyTimeProp.GetValue();
              PrintDate(date,  "", "\n");
          
         // Get the next entity
      
              anEntity = iterator.Next(propertySet);
          }   
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker