|
| 1 | +################################################################################ |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | +Expands any given file reference to a full pathname. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | +Expands any given file reference to a full pathname, with respect to the user's |
| 8 | +current directory. Can optionally assure that directories or files exist. |
| 9 | +
|
| 10 | +.PARAMETER FilePath |
| 11 | +The file path to expand to a full path. |
| 12 | +
|
| 13 | +.PARAMETER CreateDirectory |
| 14 | +Will create directory if it does not exist. |
| 15 | +
|
| 16 | +.PARAMETER CreateFile |
| 17 | +Will create an empty file if it does not exist. |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | +Expand-Path -FilePath ".\myfile.txt" -CreateFile |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | +ep ~\documents\test.txt -CreateFile |
| 24 | +#> |
| 25 | +function Expand-Path { |
| 26 | + |
| 27 | + [CmdletBinding()] |
| 28 | + [Alias("ep")] |
| 29 | + |
| 30 | + param( |
| 31 | + ######################################################################## |
| 32 | + [Parameter( |
| 33 | + Mandatory = $true, |
| 34 | + Position = 0, |
| 35 | + ValueFromPipeline = $true, |
| 36 | + ValueFromPipelineByPropertyName = $true, |
| 37 | + HelpMessage = "Path to expand" |
| 38 | + )] |
| 39 | + [ValidateNotNullOrEmpty()] |
| 40 | + [string] $FilePath, |
| 41 | + ######################################################################## |
| 42 | + [Parameter( |
| 43 | + Mandatory = $false, |
| 44 | + HelpMessage = "Will create directory if it does not exist" |
| 45 | + )] |
| 46 | + [switch] $CreateDirectory, |
| 47 | + ######################################################################## |
| 48 | + [Parameter( |
| 49 | + Mandatory = $false, |
| 50 | + HelpMessage = "Will create an empty file if it does not exist" |
| 51 | + )] |
| 52 | + [switch] $CreateFile, |
| 53 | + ######################################################################## |
| 54 | + [Parameter( |
| 55 | + Mandatory = $false, |
| 56 | + HelpMessage = "Will delete the file if it already exists" |
| 57 | + )] |
| 58 | + [switch] $DeleteExistingFile |
| 59 | + ######################################################################## |
| 60 | + ) |
| 61 | + |
| 62 | + begin { |
| 63 | + |
| 64 | + # normalize path separators and remove double separators |
| 65 | + $normalizedPath = $FilePath.Trim().Replace("\", [IO.Path]::DirectorySeparatorChar). |
| 66 | + Replace("/", [IO.Path]::DirectorySeparatorChar). |
| 67 | + Replace([IO.Path]::DirectorySeparatorChar + [IO.Path]::DirectorySeparatorChar, |
| 68 | + [IO.Path]::DirectorySeparatorChar) |
| 69 | + |
| 70 | + # check if path ends with a directory separator |
| 71 | + $hasTrailingSeparator = $normalizedPath.EndsWith( |
| 72 | + [System.IO.Path]::DirectorySeparatorChar) -or |
| 73 | + $normalizedPath.EndsWith([System.IO.Path]::AltDirectorySeparatorChar) |
| 74 | + } |
| 75 | + |
| 76 | + process { |
| 77 | + |
| 78 | + # expand home directory if path starts with ~ |
| 79 | + if ($normalizedPath.StartsWith("~")) { |
| 80 | + $normalizedPath = Join-Path (Resolve-Path ~).Path ` |
| 81 | + $normalizedPath.Substring(1) |
| 82 | + } |
| 83 | + |
| 84 | + # handle absolute paths (drive letter or UNC) |
| 85 | + if ((($normalizedPath.Length -gt 1) -and |
| 86 | + ($normalizedPath.Substring(1, 1) -eq ":")) -or |
| 87 | + $normalizedPath.StartsWith("\\")) { |
| 88 | + |
| 89 | + try { |
| 90 | + $normalizedPath = [System.IO.Path]::GetFullPath($normalizedPath) |
| 91 | + } |
| 92 | + catch { |
| 93 | + Write-Verbose "Failed to normalize path, keeping original" |
| 94 | + } |
| 95 | + } |
| 96 | + else { |
| 97 | + # handle relative paths |
| 98 | + try { |
| 99 | + $normalizedPath = [System.IO.Path]::GetFullPath( |
| 100 | + [System.IO.Path]::Combine($pwd, $normalizedPath)) |
| 101 | + } |
| 102 | + catch { |
| 103 | + $normalizedPath = Convert-Path $normalizedPath |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + # handle directory/file creation if requested |
| 108 | + if ($CreateDirectory -or $CreateFile) { |
| 109 | + |
| 110 | + # get directory path accounting for trailing separator |
| 111 | + $directoryPath = if ($hasTrailingSeparator) { |
| 112 | + [IO.Path]::TrimEndingDirectorySeparator($normalizedPath) |
| 113 | + } |
| 114 | + else { |
| 115 | + [IO.Path]::TrimEndingDirectorySeparator( |
| 116 | + [System.IO.Path]::GetDirectoryName($normalizedPath)) |
| 117 | + } |
| 118 | + |
| 119 | + # create directory if it doesn't exist |
| 120 | + if (-not [IO.Directory]::Exists($directoryPath)) { |
| 121 | + $null = [IO.Directory]::CreateDirectory($directoryPath) |
| 122 | + Write-Verbose "Created directory: $directoryPath" |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + # delete existing file if requested |
| 127 | + if ($DeleteExistingFile -and [IO.File]::Exists($normalizedPath)) { |
| 128 | + |
| 129 | + # verify path doesn't point to existing directory |
| 130 | + if ([IO.Directory]::Exists($normalizedPath)) { |
| 131 | + throw "Cannot create file: Path refers to an existing directory" |
| 132 | + } |
| 133 | + |
| 134 | + if (-not (Remove-ItemWithFallback -Path $normalizedPath)) { |
| 135 | + |
| 136 | + throw "Failed to delete existing file: $normalizedPath" |
| 137 | + } |
| 138 | + |
| 139 | + Write-Verbose "Deleted existing file: $normalizedPath" |
| 140 | + } |
| 141 | + |
| 142 | + # handle file creation if requested |
| 143 | + if ($CreateFile) { |
| 144 | + |
| 145 | + # verify path doesn't point to existing directory |
| 146 | + if ([IO.Directory]::Exists($normalizedPath)) { |
| 147 | + throw "Cannot create file: Path refers to an existing directory" |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + # create empty file if it doesn't exist |
| 152 | + if (-not [IO.File]::Exists($normalizedPath)) { |
| 153 | + $null = [IO.File]::WriteAllText($normalizedPath, "") |
| 154 | + Write-Verbose "Created empty file: $normalizedPath" |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + # clean up trailing separators except for root paths |
| 159 | + while ([IO.Path]::EndsInDirectorySeparator($normalizedPath) -and |
| 160 | + $normalizedPath.Length -gt 4) { |
| 161 | + $normalizedPath = [IO.Path]::TrimEndingDirectorySeparator($normalizedPath) |
| 162 | + } |
| 163 | + |
| 164 | + return $normalizedPath |
| 165 | + } |
| 166 | + |
| 167 | + end { |
| 168 | + } |
| 169 | +} |
| 170 | +################################################################################ |
0 commit comments