File System - Create Folders

Octopus.Script exported 2015-07-02 by jbennett belongs to ‘File System’ category.

Ensure/Create multiple folders separated by ;

Parameters

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

Folder Paths

FolderPaths

A list of folders to create separated by a ;

Continue On Error

ContinueOnError

When this is selected the script will log failures but continue and succeed in octopus.

Script body

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

# Running outside octopus
param(
    [string]$FolderPaths,
    [string]$ContinueOnError
) 

$ErrorActionPreference = "Stop" 

function Get-Param($Name, [switch]$Required, $Default) {
    $result = $null

    if ($OctopusParameters -ne $null) {
        $result = $OctopusParameters[$Name]
    }

    if ($result -eq $null) {
        $variable = Get-Variable $Name -EA SilentlyContinue   
        if ($variable -ne $null) {
            $result = $variable.Value
        }
    }

    if ($result -eq $null) {
        if ($Required) {
            throw "Missing parameter value $Name"
        } else {
            $result = $Default
        }
    }

    return $result
}

& {
    param(
        [string]$FolderPaths,
        [string]$ContinueOnError
    ) 

    Write-Host "File System - Create Folders"
    Write-Host "FolderPaths: $FolderPaths"
    
    $isContinueOnError = $ContinueOnError.ToLower() -match "(y|yes|true)"

    $FolderPaths.Split(";") | ForEach {
        $path = $_.Trim()

        if($path.Length -lt 1){
            break;
        }

        Write-Host "Trying to ensure directory structure for $path."
        try {
            $newFolder = New-Item -ItemType directory -Path $path -force
            Write-Host "SUCCESS" -ForegroundColor Green
        } catch {
            $errorMessage = "FAILED - $_.Exception.Message"
            
            if($isContinueOnError){
                Write-Host $errorMessage  -ForegroundColor Red
            } else {
                throw $errorMessage
            }
        }
        
    }

 } `
 (Get-Param 'FolderPaths' -Required) `
 (Get-Param 'ContinueOnError')

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": "7eaad6c2-fd5c-40a4-b880-350c983dc51d",
  "Name": "File System - Create Folders",
  "Description": "Ensure/Create multiple folders separated by ;",
  "Version": 7,
  "ExportedAt": "2015-07-02T13:47:00.305+00:00",
  "ActionType": "Octopus.Script",
  "Author": "jbennett",
  "Parameters": [
    {
      "Name": "FolderPaths",
      "Label": "Folder Paths",
      "HelpText": "A list of folders to create separated by a ;",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    },
    {
      "Name": "ContinueOnError",
      "Label": "Continue On Error",
      "HelpText": "When this is selected the script will log failures but continue and succeed in octopus.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Running outside octopus\nparam(\n    [string]$FolderPaths,\n    [string]$ContinueOnError\n) \n\n$ErrorActionPreference = \"Stop\" \n\nfunction Get-Param($Name, [switch]$Required, $Default) {\n    $result = $null\n\n    if ($OctopusParameters -ne $null) {\n        $result = $OctopusParameters[$Name]\n    }\n\n    if ($result -eq $null) {\n        $variable = Get-Variable $Name -EA SilentlyContinue   \n        if ($variable -ne $null) {\n            $result = $variable.Value\n        }\n    }\n\n    if ($result -eq $null) {\n        if ($Required) {\n            throw \"Missing parameter value $Name\"\n        } else {\n            $result = $Default\n        }\n    }\n\n    return $result\n}\n\n& {\n    param(\n        [string]$FolderPaths,\n        [string]$ContinueOnError\n    ) \n\n    Write-Host \"File System - Create Folders\"\n    Write-Host \"FolderPaths: $FolderPaths\"\n    \n    $isContinueOnError = $ContinueOnError.ToLower() -match \"(y|yes|true)\"\n\n    $FolderPaths.Split(\";\") | ForEach {\n        $path = $_.Trim()\n\n        if($path.Length -lt 1){\n            break;\n        }\n\n        Write-Host \"Trying to ensure directory structure for $path.\"\n        try {\n            $newFolder = New-Item -ItemType directory -Path $path -force\n            Write-Host \"SUCCESS\" -ForegroundColor Green\n        } catch {\n            $errorMessage = \"FAILED - $_.Exception.Message\"\n            \n            if($isContinueOnError){\n                Write-Host $errorMessage  -ForegroundColor Red\n            } else {\n                throw $errorMessage\n            }\n        }\n        \n    }\n\n } `\n (Get-Param 'FolderPaths' -Required) `\n (Get-Param 'ContinueOnError')",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-create-folders.json",
  "Website": "/step-templates/7eaad6c2-fd5c-40a4-b880-350c983dc51d",
  "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/LsJC4lQSkhhkQch4Qa+MgrYBahIKcB35AYrIjyEAQ9gsCm5E2GTIDQabsJysFQcZkxxj0H2LKfkGmIMjM3KgfgbBHfQSCROwnKwZByNtJsACt8WlChnRAkA7ZkaAOqyEZ0gJB7O2LRyAjsmOIutlNjnoJhD3qL9gXy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