Function: New-PWFolderPath

well, for once instead of asking for a function to be added I'm providing one if anyone is interested.

New-PWFolderPath -FolderPath <string> [-StorageArea <string>]

will create all the folders in the specified folder path that are missing, the function returns a Folder path object for the last folder in the folder path. For example:

New-PWFolderPath -FolderPath "City\Project1\Folder1\Folder2"

If folder City and Project1 already exist, then Folder1 is created, then Folder2 is created inside of Folder2. If no StorageArea is defined then the folders created will inherit from the folder above. If StorageArea is defined, ONLY folders that are created will use the defined StorageArea.

This uses a couple of medium level programming concepts - primarly recursion. Rather than writing a big foreach loop to walk the directory tree, i just call the same function again to handle the next step level of folder. Also the Splat for creating the folder uses the Add method to add the StorageArea if one is defined. If no StorageArea defined it isn't passed to New-PWFolder function. Array slices also make an appearance.

#   Function: New-PWFolderPath
# Written By: Kevin van Haaren
# Created On: 2019-03-08
#    Version: 1.0
#
# Change Log
# ##########
# 1.0 - Initial Release

Function New-PWFolderPath {
<#
	.SYNOPSIS
	Create all folders in a folder path
	
	.DESCRIPTION
	Create all folders in provided path.
	Envrionment/Workflow are assumed to be inheriting from previous folder.
	If no StorageArea is defined, new folders will inherit from folder above.
	Otherwise, defined StorageArea is used for new folders.
	No description is set.
	Returns a ProjectWise folder object to the last folder in the path.
	Assumes already logged into ProjectWise.

	.Parameter FolderPath
		Path of folders to create. Do not include Documents folder in ProjectWise
	
	.Parameter StorageArea
		Name of StorageArea to create all the folders in. If a folder already exists,
		the storage area IS NOT CHANGED. Leave out to have folders all inherit.

	.Parameter index
		Used for recursion. used to keep track of which folder is being checked/created.
	
	.EXAMPLE
	New-PWFolderPath -FolderPath "City Name\Project1\Folder1\Folder2"
	verifies each folder listed exists. If it doesn't it creates it.
#>

	[CmdletBinding()]
	Param (
		[Parameter(
			Mandatory = $True,
			Position = 0,
			ValueFromPipeline = $True,
			ValueFromPipelineByPropertyName = $True,
			HelpMessage = 'ProjectWise Folder Path to create'
		)]
		[String]$FolderPath,

		[Parameter(
			Mandatory = $false,
			ValueFromPipeline = $True,
			ValueFromPipelineByPropertyName = $True,
			HelpMessage = 'Storage Area to create folder in'
		)]
		[String]$StorageArea='',

		[Parameter(
			Mandatory = $false,
			ValueFromPipeline = $false,
			ValueFromPipelineByPropertyName = $True,
			HelpMessage = 'Zero based index, used in recursion, used to keep track of where in path currently at.'
		)]
		[Int]$index=0

)

	# break up path into pieces
	$PWPath = $FolderPath -replace '/','\'
	$pieces = $PWPath.Split('\')
	
	$testfolder=$pieces[0..$index] -join '\'
	# check if first folder exists
	try {
		$results = Get-PWFolders -FolderPath $testfolder -JustOne -ErrorAction Stop
	} catch {
		$results = $false
	}
	
	#region if folder does not exist, create it, then skip to next one
	if ($results -eq $false) {
		$FolderSplat = @{
			'FolderPath' = $TestFolder
			'ErrorAction' = 'Stop'
		}
		if ($StorageArea -ne '') {
			$FolderSplat.Add('StorageArea',$StorageArea)
		}
		try {
			$results = New-PWFolder @FolderSplat
		} catch {
			throw "Error creating folder $testfolder"
		}
	}
	#endregion
	
	#region process next folder, or return
	$index++
	if ($index -lt $pieces.count) {
		$Folder = New-PWFolderPath -FolderPath $PWPath -index $index
		return $Folder
	} else {
		return (Get-PWFolders -FolderPath $testfolder -JustOne -Slow)
	}
	#endregion
}

Parents
  • man i am a dummy. New-PWFolder works this way too. I could've sworn I tested that last week and it would only work if all but the last folder existed.

    Crud, that's some lost time.

    Anyway, i'll leave this here for others that want to see how recursion works.

     

Reply Children
No Data