# Copy

Required Frends version: 5.5+

Required .NET version: 6.0

Compatible Agents: Crossplatform

Source code: <https://github.com/FrendsPlatform/Frends.Files/tree/main/Frends.Files.Copy>

{% tabs %}
{% tab title="Parameter: Input" %}

| Name                       | Description                                                                                                                                                                                                                                                  |                                                               |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------- |
| Directory : `String`       | <p>Source directory.</p><p><br>Default: <code>-</code><br>Example: <code>c:\temp</code></p>                                                                                                                                                                  |                                                               |
| Pattern : `String`         | <p>Pattern to match for files. The file mask uses regular expressions, but for convenience, it has special handling for \* and ? wildcards.</p><p><br>Default: <code>"\*\*\Folder\*.xml"</code><br>Example: <code>test.txt, test\*.txt, test?.txt, test.(txt | xml), test.\[^t]\[^x]\[^t], ^(?!prof).\*\_test.txt</code></p> |
| TargetDirectory : `String` | <p>Target directory where the found files should be copied to</p><p><br>Default: <code>"d:\backup"</code><br>Example: <code>d:\backup\</code></code></p>                                                                                                     |                                                               |
| {% endtab %}               |                                                                                                                                                                                                                                                              |                                                               |

{% tab title="Parameter: Options" %}

| Name                                                    | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| UseGivenUserCredentialsForRemoteConnections : `Boolean` | <p>If set, allows you to give the user credentials to use to delete files on remote hosts.</p><p>If not set, the agent service user credentials will be used.</p><p>Note: This feature is only possible with Windows agents.</p><p><br>Default: <code>False</code><br>Example: <code>true</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| UserName : `String`                                     | <p>This needs to be of format domain\username</p><p><br>Default: <code>"domain\username"</code><br>Example: <code>domain\username</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| 🗝Password : `String`                                   | <p>Password for the used credentials.</p><p><br>Default: <code>-</code><br>Example: <code>testpwd</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| PreserveDirectoryStructure : `Boolean`                  | <p>If set, will recreate the directory structure from the SourceDirectory under the TargetDirectory for copied files</p><p><br>Default: <code>False</code><br>Example: <code>true</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| CreateTargetDirectories : `Boolean`                     | <p>If set, will create the target directory if it does not exist,</p><p>as well as any sub directories if is set.</p><p><br>Default: <code>True</code><br>Example: <code>true</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| IfTargetFileExists : `FileExistsAction`                 | <p>What should happen if a file with the same name already exists in the target directory.</p><p>\* Throw - Throw an error and roll back all transfers</p><p>\* Overwrite - Overwrites the target file</p><p>\* Rename - Renames the transferred file by appending a number to the end</p><p>Possible values:</p><ul><li><code>Throw</code>:</li></ul><p>What should happen if a file with the same name already exists in the target directory.</p><p>\* Throw - Throw an error and roll back all transfers</p><p>\* Overwrite - Overwrites the target file</p><p>\* Rename - Renames the transferred file by appending a number to the end</p><ul><li><code>Overwrite</code>:</li></ul><p>What should happen if a file with the same name already exists in the target directory.</p><p>\* Throw - Throw an error and roll back all transfers</p><p>\* Overwrite - Overwrites the target file</p><p>\* Rename - Renames the transferred file by appending a number to the end</p><ul><li><code>Rename</code>:</li></ul><p>What should happen if a file with the same name already exists in the target directory.</p><p>\* Throw - Throw an error and roll back all transfers</p><p>\* Overwrite - Overwrites the target file</p><p>\* Rename - Renames the transferred file by appending a number to the end</p><p><br>Default: <code>-</code><br>Example: <code>FileExistsAction.Overwrite</code></p> |
| ThrowErrorOnFail : `Boolean`                            | <p>Whether to throw an error on a failure to copy a file.</p><p>This option is independent of the option with the value of and will affect handling of all errors during the copy process.</p><p>If set to false, task will continue executing despite files failing to copy, otherwise execution will stop at the first failure.</p><p>Note: When set to false and is set to , the task will continue copying other files despite a failure.</p><p>Behaviour of and will not be affected by this setting.</p><p><br>Default: <code>True</code><br>Example: <code>true</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| {% endtab %}                                            |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

{% tab title="Result: Result" %}

| Name                                 | Description                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Files : `List<FileItem>`             | <p>List of FileItems including source directory and target directory.<br>Example: <code>\[object {SourcePath: C:\test\testfolder\test1.txt, TargetPath: C:\test\moved\test1.txt}, object {SourcePath: C:\test\testfolder\test2.txt, TargetPath: C:\test\moved\test2.txt}]</code></p>                                                                                                                                             |
| FailedFiles : `List<FailedFileItem>` | <p>List of FailedItems including path of the source file and failure exception. This list will always be empty unless is set to false.<br>Example: <code>\[object {SourcePath: C:\test\testfolder\test1.txt, Exception: object {Message: Unable to create 'C:\test\moved' directory}}, object {SourcePath: C:\test\testfolder\test2.txt, Exception: object {Message: File 'C:\test\moved\test2.txt' already exists}}]</code></p> |
| {% endtab %}                         |                                                                                                                                                                                                                                                                                                                                                                                                                                  |

{% tab title="Changelog" %}

## Changelog

### \[1.3.0] - 2025-03-19

#### Changed

* Update packages: Microsoft.Extensions.FileSystemGlobbing 7.0.0 -> 9.0.3 System.ComponentModel.Annotations 4.7.0 -> 5.0.0 System.DirectoryServices 7.0.0 -> 8.0.0 coverlet.collector 3.1.0 -> 6.0.4 Microsoft.NET.Test.Sdk 16.6.1 -> 17.13.0 MSTest.TestAdapter 2.2.7 -> 3.8.3 MSTest.TestFramework 2.2.8 -> 3.8.3 nunit 3.12.0 -> 4.3.2 NUnit3TestAdapter 3.17.0 -> 5.0.0

### \[1.2.0] - 2025-04-08

#### Added

* New option ThrowErrorOnFail to allow task to continue despite some files failing to copy.
  * Files that failed to copy will be listed in the Result.FailedFiles property.
  * When set to false and IfTargetFileExists is set to Throw, the task will continue copying other files despite an error.

### \[1.1.0] - 2025-03-13

#### Fixed

* Fixed bug where regex patterns were not handled correctly in file matching.

### \[1.0.2] - 2023-11-27

#### Changed

* Documentational changes.

### \[1.0.1] - 2023-09-21

#### Fixed

* Fixed bug with file access. Changed File.Open to use FileAccess.Read, because there's no reason to give more access to the Task.

### \[1.0.0] - 2023-02-14

#### Added

* Initial implementation
  {% endtab %}
  {% endtabs %}
