File System - Rename File

Octopus.Script exported 2017-06-01 by daviesaus belongs to ‘File System’ category.

Renames a file on 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 Path

FilePath =

The location of the file you wish to rename.

New Name

NewName =

The new name for the file you’re renaming.

Script body

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

$filePath = $OctopusParameters['FilePath']
$newName = $OctopusParameters['NewName']

function Test-FileLocked([string]$filePath)
{
  Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
  return ($errs.Count -ne 0)
}

$fileExists = Test-Path -Path $filePath

if (!$fileExists)
{
  Write-Warning "File not found at $filePath"

  return
}

$fileIsLocked = Test-FileLocked($filePath)

function Wait-ForFileUnlock
{
  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 "File at location $filePath is locked and cannot be renamed"

      return
    }
  }
}

if ($fileIsLocked)
{
  Wait-ForFileUnlock
}

Rename-Item -Path $filePath -NewName $newName

Write-Host "Successfully renamed file at location: $filePath to $newName"

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": "5ca37bfb-ebbc-4b4c-ab1e-06d462e7f910",
  "Name": "File System - Rename File",
  "Description": "Renames a file on the file system.",
  "Version": 2,
  "ExportedAt": "2017-06-01T11:11:00.134Z",
  "ActionType": "Octopus.Script",
  "Author": "daviesaus",
  "Parameters": [
    {
      "Id": "37fd875e-f5f7-493c-bb8c-30354bf39678",
      "Name": "FilePath",
      "Type": "String",
      "Label": "File Path",
      "HelpText": "The location of the file you wish to rename.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      },
      "Links": {}
    },
    {
      "Id": "facc1261-1b0e-476e-a520-13e682c4f8b7",
      "Name": "NewName",
      "Type": "String",
      "Label": "New Name",
      "HelpText": "The new name for the file you're renaming.",
      "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": "$filePath = $OctopusParameters['FilePath']\n$newName = $OctopusParameters['NewName']\n\nfunction Test-FileLocked([string]$filePath)\n{\n  Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue\n  return ($errs.Count -ne 0)\n}\n\n$fileExists = Test-Path -Path $filePath\n\nif (!$fileExists)\n{\n  Write-Warning \"File not found at $filePath\"\n\n  return\n}\n\n$fileIsLocked = Test-FileLocked($filePath)\n\nfunction Wait-ForFileUnlock\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 \"File at location $filePath is locked and cannot be renamed\"\n\n      return\n    }\n  }\n}\n\nif ($fileIsLocked)\n{\n  Wait-ForFileUnlock\n}\n\nRename-Item -Path $filePath -NewName $newName\n\nWrite-Host \"Successfully renamed file at location: $filePath to $newName\"",
    "Octopus.Action.Script.ScriptFileName": null,
    "Octopus.Action.Package.FeedId": null,
    "Octopus.Action.Package.PackageId": null
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-rename-file.json",
  "Website": "/step-templates/5ca37bfb-ebbc-4b4c-ab1e-06d462e7f910",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRT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