<copy>

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

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 NAnt.Core.Tasks.CopyTask.Overwrite attribute.

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

Files that are not located under the the base directory of the NAnt.Core.Types.FileSet will be copied directly under to the destination directory, regardless of the value of the NAnt.Core.Tasks.CopyTask.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.

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.

Parameters

Attribute Type Description Required
flatten bool
Ignore directory structure of source directory, copy all files into a single directory, specified by the NAnt.Core.Tasks.CopyTask.ToDirectory attribute. The default is false.
False
includeemptydirs bool
Copy any empty directories included in the NAnt.Core.Types.FileSet. The default is true.
False
inputencoding
The encoding to use when reading files. The default is the system's current ANSI code page.
False
outputencoding
The encoding to use when writing the files. The default is the encoding of the input file.
False
overwrite bool
Overwrite existing files even if the destination files are newer. The default is false.
False
file file
The file to copy.
False
todir directory
The directory to copy to.
False
tofile file
The file to copy to.
False
failonerror bool
Determines if task failure stops the build, or is just reported. The default is true.
False
if bool
If true then the task will be executed; otherwise, skipped. The default is true.
False
unless bool
Opposite of NAnt.Core.Task.IfDefined. If false then the task will be executed; otherwise, skipped. The default is false.
False
verbose bool
Determines whether the task should report detailed build log messages. The default is false.
False

Nested elements

<fileset>

Used to select the files to copy. To use a NAnt.Core.Types.FileSet, the NAnt.Core.Tasks.CopyTask.ToDirectory attribute must be set.

</fileset>

<filterchain>

Chain of filters used to alter the file's content as it is copied.

</filterchain>

Examples

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 tofile="target/dir">
  <fileset basedir="source/dir"/>
</copy>

Requirements

Assembly
NAnt.Core.dll
Namespace
NAnt.Core.Tasks