Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
MSBuild Reference
 Copy Task

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework General Reference
Copy Task

Copies files on the filesystem to a new location.

The folowing table describes the parameters of the Copy task.

Parameter

Description

CopiedFiles

Optional ITaskItem[] output parameter.

Contains the items that were successfully copied.

DestinationFiles

Optional ITaskItem[] parameter.

Specifies the list of files to copy the source files to. This list is expected to be a one-to-one mapping with the list specified in the SourceFiles parameter. That is, the first file specified in SourceFiles will be copied to the first location specified in DestinationFiles, and so forth.

DestinationFolder

Optional ITaskItem parameter.

Specifies the directory to which you want to copy the files. This must be a directory, not a file. If the directory does not exist, it is created automatically.

OverwriteReadOnlyFiles

Optional Boolean parameter.

Overwrite files even if they are marked as read only files

SkipUnchangedFiles

Optional Boolean parameter.

If true, skips the copying of files that are unchanged between the source and destination. The Copy task considers files to be unchanged if they have the same size and the same last modified time.

NoteNote:
If you set this parameter to true, you should not use dependency analysis on the containing target, because that only runs the task if the last-modified times of the source files are newer than the last-modified times of the destination files.

SourceFiles

Required ITaskItem[] parameter.

Specifies the files to copy.

Either the DestinationFolder or the DestinationFiles parameter must be specified, but not both. If both are specified, the task fails and an error is logged.

The following example copies the items in the MySourceFiles item collection into the folder c:\MyProject\Destination.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <MySourceFiles Include="a.cs;b.cs;c.cs"/>
    </ItemGroup>

    <Target Name="CopyFiles">
        <Copy
            SourceFiles="@(MySourceFiles)"
            DestinationFolder="c:\MyProject\Destination"
        />
    </Target>

</Project>

The following example demonstrates how to do a recursive copy. This project copies all of the files recursively from c:\MySourceTree into c:\MyDestinationTree, while maintaining the directory structure.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <MySourceFiles Include="c:\MySourceTree\**\*.*"/>
    </ItemGroup>

    <Target Name="CopyFiles">
        <Copy
            SourceFiles="@(MySourceFiles)"
            DestinationFiles="@(MySourceFiles->'c:\MyDestinationTree\%(RecursiveDir)%(Filename)%(Extension)')"
        />
    </Target>

</Project>

Concepts

Other Resources

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Copy task does not copy over empty folders      rajeevacharya ... Thomas Lee   |   Edit   |   Show History
If your source folders have any empty folders in them, and you use the "Recursive" example, then any empty folders will not get created/copied over to the destination. Add a dummy file in the empty source folder to enable it being copied over/created at the destination.
Flag as ContentBug
MSBuild Framework 3.5 doesn't recognize OverwriteReadOnlyFiles option.      kenmba   |   Edit   |   Show History

<Copy SourceFiles="c:\Test1\aaa.txt" DestinationFolder="c:\Test2" OverwriteReadOnlyFiles="true" />

To get this code to work, you need to put ToolsVersion="3.5" in <Project> tag.

Tags What's this?: Add a tag
Flag as ContentBug
need to document the recursive syntax better      Steve Nuchia   |   Edit   |   Show History
The example indicates that \**\ is special, triggering recursive enumeration of a file tree. But I've found that any wildcard in a folder name triggers the same treatment. I didn't want it to, but it did. No clue in the available documentation that this was going to happen.
Tags What's this?: Add a tag
Flag as ContentBug
how to call copy task from c# code?      Shekhar M   |   Edit   |   Show History
hii plz anybody suggest me how to call copy task thrugh c#?
OverwriteReadOnlyFiles option not recognized for copy task      chingu   |   Edit   |   Show History
NEVERMIND, I just didn't set the ToolsVersion attribute at a high enough level. I didn't realize it had to be set at the top level project tag for it to work in the build.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker