NAnt SDK Documentation - v0.92 |

# CopyTask Class

Copies a file, a directory, or set of files to a new file or directory.

For a list of all members of this type, see CopyTask Members.

System.Object
NAnt.Core.Element
NAnt.Core.Task
NAnt.Core.Tasks.CopyTask
NAnt.Core.Tasks.MoveTask

[Visual Basic]
<TaskName(Name:="copy")>
Public Class CopyTask
Inherits Task
[C#]
[TaskName(Name="copy")]
public class CopyTask : Task

#### Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

#### Remarks

Files are only copied if the source file is newer than the destination file, or if the destination file does not exist. However, you can explicitly overwrite files with the Overwrite attribute.

Entire directory structures can be copied to a new location. For this to happen, the following criteria must be met:

- Everything in the fileset is included
- The directory structure is not flattened
- Empty directories are included
- Destination directory does not exist

If any of these items are not met, then the files within the source directory will be copied over instead of the entire directory structure.

When a FileSet is used to select files or directories to copy, the ToDirectory attribute must be set. Files that are located under the base directory of the FileSet will be copied to a directory under the destination directory matching the path relative to the base directory of the FileSet, unless the Flatten attribute is set to true.

Files that are not located under the the base directory of the FileSet will be copied directly under to the destination directory, regardless of the value of the Flatten attribute.

#### Encoding

Unless an encoding is specified, the encoding associated with the system's current ANSI code page is used.

An UTF-8, little-endian Unicode, and big-endian Unicode encoded text file is automatically recognized, if the file starts with the appropriate byte order marks.

> Note If you employ filters in your copy operation, you should limit the copy to text files. Binary files will be corrupted by the copy operation.

#### Example

Copy a single file while changing its encoding from "latin1" to "utf-8".

    <copy
        file="myfile.txt"
        tofile="mycopy.txt"
        inputencoding="latin1"
        outputencoding="utf-8" />

Copy a set of files to a new directory.

    <copy todir="${build.dir}">
        <fileset basedir="bin">
            <include name="*.dll" />
        </fileset>
    </copy>

Copy a set of files to a directory, replacing `@TITLE@` with "Foo Bar" in all files.

    <copy todir="../backup/dir">
        <fileset basedir="src_dir">
            <include name="**/*" />
        </fileset>
        <filterchain>
            <replacetokens>
                <token key="TITLE" value="Foo Bar" />
            </replacetokens>
        </filterchain>
    </copy>

Copy an entire directory and its contents.

    <copy todir="target/dir">
      <fileset basedir="source/dir"/>
    </copy>

#### Requirements

Namespace: NAnt.Core.Tasks

Assembly: NAnt.Core (in NAnt.Core.dll)

#### See Also

CopyTask Members | NAnt.Core.Tasks Namespace