File System - Copy File

Octopus.Script exported 2018-03-16 by Jens-H-Eriksen belongs to ‘File System’ category.

Copies a file in the file system.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

File source path

sourcePath =

The full path of the file you wish to copy.

File destionation path

destinationPath =

The full path for the destination of the file. You can rename the file by giving it a new name in the path. If the file already exists at the destination it is overwritten.

Script body

Steps based on this template will execute the following PowerShell script.

function Test-FileLocked([string]$filePath)
{
  $fileExists = Test-Path -Path $filePath
  if (!$fileExists)
  {
    return (1 -eq 0) #false
  }
  Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
  return ($errs.Count -ne 0)
}

function Wait-ForFileUnlock([string]$filePath)
{ 
  Write-Host "Destinationfile at $filePath is locked"
    
  for ($attemptNo = 1; $attemptNo -lt 6; $attemptNo++) {
    Write-Host "Waiting for the file to become unlocked $attemptNo/5"

    Start-Sleep -Seconds 10

    $fileIsLocked = Test-FileLocked($filePath)

    if (!$fileIsLocked)
    {
      return
    }

    if ($attemptNo -eq 5) {
      Write-Error "destinationfile at location $filePath is locked and cannot be overwritten."

      return
    }
  }
}

#
#script starts here
#

$filePath = $OctopusParameters['sourcePath']
$newFilePath = $OctopusParameters['destinationPath']

$fileExists = Test-Path -Path $filePath

if (!$fileExists)
{
  Write-Error "Sourcefile not found at $filePath"

  return
}

$fileIsLocked = Test-FileLocked($newFilePath)


if ($fileIsLocked)
{
  Wait-ForFileUnlock($newFilePath)
}


$fileIsLocked = Test-FileLocked($newFilePath)
if ($fileIsLocked)
{
    return
}

Copy-Item -Path $filePath -Destination $newFilePath -Force

Write-Host "Successfully copied file at location: $filePath to $newFilePath"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "072c2939-2c77-4ed1-abd2-e41cb8a57661",
  "Name": "File System - Copy File",
  "Description": "Copies a file in the file system.",
  "Version": 6,
  "ExportedAt": "2018-03-16T12:58:03.401Z",
  "ActionType": "Octopus.Script",
  "Author": "Jens-H-Eriksen",
  "Parameters": [
    {
      "Id": "80271c96-5190-4cc6-93c0-6e28c4aba6f1",
      "Name": "sourcePath",
      "Label": "File source path",
      "HelpText": "The full path of the file you wish to copy.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "702e4cfd-6633-4206-94df-8fa4fa47e564",
      "Name": "destinationPath",
      "Label": "File destionation path",
      "HelpText": "The full path for the destination of the file. You can rename the file by giving it a new name in the path. If the file already exists at the destination it is overwritten.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false",
    "Octopus.Action.Script.ScriptBody": "function Test-FileLocked([string]$filePath)\n{\n  $fileExists = Test-Path -Path $filePath\n  if (!$fileExists)\n  {\n    return (1 -eq 0) #false\n  }\n  Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue\n  return ($errs.Count -ne 0)\n}\n\nfunction Wait-ForFileUnlock([string]$filePath)\n{ \n  Write-Host \"Destinationfile at $filePath is locked\"\n    \n  for ($attemptNo = 1; $attemptNo -lt 6; $attemptNo++) {\n    Write-Host \"Waiting for the file to become unlocked $attemptNo/5\"\n\n    Start-Sleep -Seconds 10\n\n    $fileIsLocked = Test-FileLocked($filePath)\n\n    if (!$fileIsLocked)\n    {\n      return\n    }\n\n    if ($attemptNo -eq 5) {\n      Write-Error \"destinationfile at location $filePath is locked and cannot be overwritten.\"\n\n      return\n    }\n  }\n}\n\n#\n#script starts here\n#\n\n$filePath = $OctopusParameters['sourcePath']\n$newFilePath = $OctopusParameters['destinationPath']\n\n$fileExists = Test-Path -Path $filePath\n\nif (!$fileExists)\n{\n  Write-Error \"Sourcefile not found at $filePath\"\n\n  return\n}\n\n$fileIsLocked = Test-FileLocked($newFilePath)\n\n\nif ($fileIsLocked)\n{\n  Wait-ForFileUnlock($newFilePath)\n}\n\n\n$fileIsLocked = Test-FileLocked($newFilePath)\nif ($fileIsLocked)\n{\n    return\n}\n\nCopy-Item -Path $filePath -Destination $newFilePath -Force\n\nWrite-Host \"Successfully copied file at location: $filePath to $newFilePath\""
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-copy-file.json",
  "Website": "/step-templates/072c2939-2c77-4ed1-abd2-e41cb8a57661",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKhQTFRF/////78A/6oAVVVV/++//+q/gICA1dXV/79A/9R///ff/8MQ/9dg/8cg//vv/68A/+ef//PP//rv/7Ug/68Q/+Sv/8sw/9tw/9NQ/89w/+uv/+OP/8pg/89A/8VQ/9+f/9+A/9qP/+/P/99//7cA/7IA/7wA/6sA/+/A39/f/64A2tjX//Tfn5+f/7MA/7sA/74A/60A/9Zw/7gA/74Q/7oA/7UA/7EAi6g4fwAAAuRJREFUeNrs21tT2mAUheEvaaCQQCSczyi29WzP7f//Z6UdZBIIM44us9hf13vbC+eRbLo3qnNKKaWUUkoppZRSSimllFJKKaWUwlVrLhsvbrLqnoai2+yFr2x+fwKOwasZm/oXdEczxDQnOyYhqgn3uQpxDZhz3gdCesQ3r3mIjDfwXagj7NEgKywkXLMgSzCE9mz1wBDaOzDYETYEEUSQ/wPSaz6rQa174pDnt1x5Atm8emtPIGG48gUClJAh/XtPILi36+L3Zz6oVdBF4w32/sIbYmWH6qAPX5fzjio/14Q/W7nnqtIPDnYfFfThkGpPovXu68IhNdKSVyXkoQ7qgQypvwNVFwQDuXkP6oYM8WbYBWFDYMPNHnZBTv3tV8MuiCC+Q6K36+ypkn/LsJC4lQSkhhkQch4Q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