### Copying a File or Directory Using IFileSystemEntity::copyTo

IFileSystemEntity provides the copyTo function, inherited by IFile and IDirectory, which you can use to copy an entity from one parent directory to another:

    IDirectory destinationDir = ...;
    aFile.copyTo(destinationDir);
    aDirectory.copyTo(destinationDir);

You can specify a new name for the copy:

    aFile.copyTo(destinationDir, newName);
    aDirectory.copyTo(destinationDir, IFileName(""));

If the new name you provide is empty, the original entity's name is used.

If you are copying a directory tree, you can control the behavior when an error occurs. You have two choices for error handling, as defined by the IFileSystemEntity::EFailureAction enum:

- kStop--causes the exception to be thrown immediately. This is the default.
- kContinue--attempts to complete the operation on all remaining entities, and then throws the exception encountered.

For example:

    aDirectory.copyTo(destinationDir, IFileName(""), IDirectory::kContinue);

Note: Since files do not have children, the kContinue flag has no effect if an error occurs while copying a file.

### Moving a File or Directory Using IFileSystemEntity::moveTo

IFileSystemEntity provides the moveTo function, inherited by IFile and IDirectory, which you can use to move an entity from one parent directory to another:

    IDirectory destinationDir = ...;
    aFile.moveTo(destinationDir);
    aDirectory.moveTo(destinationDir);

Note: If you want to move a physical entity to a different name in the same directory (rename it), you can use the setName function.

You can specify a new name for the directory at the same time you move it:

    aFile.moveTo(destinationDir, newName);
    aDirectory.moveTo(destinationDir, IFileName(""));

If the new name you provide is empty, the original entity's name is used.

If you are moving a directory tree, you can control the behavior when an error occurs. See Copying a File or Directory Using IFileSystemEntity::copyTo for information about the available options. For example:

    aDirectory.moveTo(destinationDir, IFileName(""), IDirectory::kContinue);

Note: Since files do not have children, the kContinue flag has no effect if an error occurs while moving a file.

When you call moveTo on an entity instance, the instance will be updated to point to the entity's new physical location on disk. However, any other instances that pointed to the original physical entity will not be updated.

### Using the IFileSystemCopier and IFileSystemMover Classes Directly

You can use the IFileSystemCopier and the IFileSystemMover utility classes to copy and move file system entities. Simply instantiate the appropriate object, specifying whether to stop or keep going when a failure occurs, and then call its move or copy function:

    IFileSystemCopier copier(IFileSystemCopier::kContinue);
    copier.copy(aFile, destinationDir);

    IFileSystemMover mover(IFileSystemMover::kStop);
    mover.move(aFile, destinationDir, newName);
    mover.move(aDir, destinationDir);

Introduction to the File System
Overview of File System Entities
IFileSystemEntity's moveTo and copyTo Functions
Mover and Copier Classes

Instantiating a File System Entity from a Host-Specific Pathname
Instantiating a File System Entity from a Portable Pathname
Supporting Progress Indicators
Resolving Filename Conflicts
Handling Exceptions and Other Failures
Creating Your Own File System Operation