Directories and TDirectory

A TDirectory instance is a surrogate entity that maps to a physical directory. Using a TDirectory instance you can look up an entity within a directory, create a new directory, create a new file, delete the contents of a directory, and delete the directory itself.

Looking up entities within a directory

TDirectory provides two look-up functions: LookUp and TreeLookUp. These functions are similar to the iterators TDirectoryIterator and TDirectoryTreeIterator, with one key distinction--LookUp and TreeLookUp stop iterating through a directory once they find a match. In contrast, TDirectoryIterator and TDirectoryTreeIterator iterate over an entire directory or directory tree until complete and they return every match.

As with iterators, look-up functions take a TFileSystemEntityName, a property query, or a property set. LookUp searches only through the entities within the current directory, and does not explore subdirectories. TreeLookUp moves through the entire directory tree beneath the directory represented by the TDirectory instance.

Creating a new directory

To create a new directory, call TDirectory::CreateDirectory for the directory in which you want the new directory to appear. This member function creates the physical entity and returns a TDirectory instance.

CreateDirectory takes a directory name, a property set containing any properties you want to initialize, and a count of the initial members. The directory name is the only required parameter.

Creating a new file

To create a new file, call TDirectory::CreateFile, passing in the filename as a TFileSystemEntityName instance. Note that if a slash (/) is embedded in the TFileSystemEntityName, the slash is treated as a literal, and not as a path separator. You can optionally specify any file properties you want to initialize at file creation time (as a TPropertySet) and the initial member count.

For details of how to create a file when you do not have a TDirectory instance, see "Files and TFile" on page 52.

Deleting directories and their contents

To completely delete the contents of a directory, you call TDirectory::DeleteAllContents for the instance you want to modify. This member function attempts to delete each member of the directory. If DeleteAllContents gets write access to all files and directories within the current directory, it deletes them all and leaves the starting directory intact. If the function cannot get write access to one or more entities (files or directories), it first completes the delete operation for all accessible entities and then throws an exception.

DeleteSelf (inherited from TFileSystemEntity) attempts to delete only the directory represented by the instance. If the directory contains members, DeleteSelf throws an exception. If, however, you pass True as a parameter, DeleteSelf attempts to delete all files and directories within the directory and then deletes itself.


[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