CopyFiles@2 - Copy files v2 task

Use this task to copy files from a source folder to a target folder using match patterns. (The match patterns will only match file paths, not folder paths).

Syntax

# Copy files v2
# Copy files from a source folder to a target folder using patterns matching file paths (not folder paths).
- task: CopyFiles@2
  inputs:
    #SourceFolder: # string. Source Folder. 
    Contents: '**' # string. Required. Contents. Default: **.
    TargetFolder: # string. Required. Target Folder. 
  # Advanced
    #CleanTargetFolder: false # boolean. Clean Target Folder. Default: false.
    #OverWrite: false # boolean. Overwrite. Default: false.
    #flattenFolders: false # boolean. Flatten Folders. Default: false.
    #preserveTimestamp: false # boolean. Preserve Target Timestamp. Default: false.
    #retryCount: '0' # string. Retry count to copy the file. Default: 0.
    #delayBetweenRetries: '1000' # string. Delay between two retries. Default: 1000.
    #ignoreMakeDirErrors: false # boolean. Ignore errors during creation of target folder. Default: false.
# Copy files v2
# Copy files from a source folder to a target folder using patterns matching file paths (not folder paths).
- task: CopyFiles@2
  inputs:
    #SourceFolder: # string. Source Folder. 
    Contents: '**' # string. Required. Contents. Default: **.
    TargetFolder: # string. Required. Target Folder. 
  # Advanced
    #CleanTargetFolder: false # boolean. Clean Target Folder. Default: false.
    #OverWrite: false # boolean. Overwrite. Default: false.
    #flattenFolders: false # boolean. Flatten Folders. Default: false.
    #preserveTimestamp: false # boolean. Preserve Target Timestamp. Default: false.
# Copy Files v2
# Copy files from source folder to target folder using match patterns (The match patterns will only match file paths, not folder paths).
- task: CopyFiles@2
  inputs:
    #SourceFolder: # string. Source Folder. 
    Contents: '**' # string. Required. Contents. Default: **.
    TargetFolder: # string. Required. Target Folder. 
  # Advanced
    #CleanTargetFolder: false # boolean. Clean Target Folder. Default: false.
    #OverWrite: false # boolean. Overwrite. Default: false.
    #flattenFolders: false # boolean. Flatten Folders. Default: false.

Inputs

SourceFolder - Source Folder
string.

Optional. The folder that contains the files you want to copy. If the folder is empty, then the task copies files from the root folder of the repo as though $(Build.SourcesDirectory) was specified.

If your build produces artifacts outside of the sources directory, specify $(Agent.BuildDirectory) to copy files from the directory created for the pipeline.


Contents - Contents
string. Required. Default value: **.

The file paths to include as part of the copy. This string supports multiple lines of match patterns.

For example:

  • * copies all files in the specified source folder.
  • ** copies all files in the specified source folder and all files in all sub-folders.
  • **\bin\** copies all files recursively from any bin folder.

The pattern is used to match only file paths, not folder paths. Specify patterns, such as **\bin\** instead of **\bin.

Use the path separator that matches your build agent type. For example, / must be used for Linux agents. More examples are shown below.


TargetFolder - Target Folder
string. Required.

The target folder or UNC path that will contain the copied files. You can use variables. Example: $(build.artifactstagingdirectory).


CleanTargetFolder - Clean Target Folder
boolean. Default value: false.

Optional. Deletes all existing files in the target folder before the copy process.


OverWrite - Overwrite
boolean. Default value: false.

Optional. Replaces the existing files in the target folder.


flattenFolders - Flatten Folders
boolean. Default value: false.

Optional. Flattens the folder structure and copies all files into the specified target folder.


preserveTimestamp - Preserve Target Timestamp
boolean. Default value: false.

Preserves the target file timestamp by using the original source file.


retryCount - Retry count to copy the file
string. Default value: 0.

Specifies the retry count to copy the file. This string is useful for intermittent issues, such as UNC target paths on a remote host.


delayBetweenRetries - Delay between two retries.
string. Default value: 1000.

Specifies the delay between two retries. This string is useful for intermittent issues, such as UNC target paths on a remote host.


ignoreMakeDirErrors - Ignore errors during creation of target folder.
boolean. Default value: false.

Ignores errors that occur during the creation of the target folder. This string is useful for avoiding issues with the parallel execution of tasks by several agents within one target folder.


Task control options

All tasks have control options in addition to their task inputs. For more information, see Control options and common task properties.

Output variables

None.

Remarks

If no files match, the task will still report success.

  • If Overwrite is false and a matched file already exists in the target folder, the task will not report failure but log that the file already exists and skip it.
  • If Overwrite is true and a matched file already exists in the target folder, the matched file will be overwritten.

Examples

Copy file to artifacts staging directory and publish

steps:
- task: CopyFiles@2
  inputs:
    contents: '_buildOutput/**'
    targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: $(Build.ArtifactStagingDirectory)
    artifactName: MyBuildOutputs

Copy executables and a readme file

Goal

You want to copy just the readme and the files needed to run this C# console app:

`-- ConsoleApplication1
    |-- ConsoleApplication1.sln
    |-- readme.txt
    `-- ClassLibrary1
        |-- ClassLibrary1.csproj
    `-- ClassLibrary2
        |-- ClassLibrary2.csproj
    `-- ConsoleApplication1
        |-- ConsoleApplication1.csproj

Note

ConsoleApplication1.sln contains a bin folder with .dll and .exe files, see the Results below to see what gets moved!

On the Variables tab, $(BuildConfiguration) is set to release.

Example with multiple match patterns:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    Contents: |
      ConsoleApplication1\ConsoleApplication1\bin\**\*.exe
      ConsoleApplication1\ConsoleApplication1\bin\**\*.dll
      ConsoleApplication1\readme.txt
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Example with OR condition:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    Contents: |
      ConsoleApplication1\ConsoleApplication1\bin\**\?(*.exe|*.dll)
      ConsoleApplication1\readme.txt
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Example with NOT condition:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    Contents: |
      ConsoleApplication1\**\bin\**\!(*.pdb|*.config)
      !ConsoleApplication1\**\ClassLibrary*\**
      ConsoleApplication1\readme.txt
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Example with variables in content section

- task: CopyFiles@2
  inputs:
    Contents: '$(Build.Repository.LocalPath)/**' 
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Results

These files are copied to the staging directory:

`-- ConsoleApplication1
    |-- readme.txt
    `-- ConsoleApplication1
        `-- bin
            `-- Release
                | -- ClassLibrary1.dll
                | -- ClassLibrary2.dll
                | -- ConsoleApplication1.exe

Copy everything from the source directory except the .git folder

Example with multiple match patterns:

steps:
- task: CopyFiles@2
  displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
  inputs:
    SourceFolder: '$(Build.SourcesDirectory)'
    Contents: |
      **/*
      !.git/**/*
    TargetFolder: '$(Build.ArtifactStagingDirectory)'

Requirements

Requirement Description
Pipeline types YAML, Classic build
Runs on Agent, DeploymentGroup
Demands None
Capabilities This task does not satisfy any demands for subsequent tasks in the job.
Command restrictions This task runs using the following command restrictions: restricted
Settable variables This task has permission to set the following variables: Setting variables is disabled
Agent version 2.182.1 or greater
Task category Utility
Requirement Description
Pipeline types YAML, Classic build
Runs on Agent, DeploymentGroup
Demands None
Capabilities This task does not satisfy any demands for subsequent tasks in the job.
Command restrictions Any
Settable variables Any
Agent version 1.91.0 or greater
Task category Utility

See also