|
| 1 | + |
| 2 | +############################################################################### |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | +Expands input objects into file and directory names, supporting various |
| 6 | +filters and output options. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | +This function processes input objects (files, directories, or collections) |
| 10 | +and expands them into file and directory names. It supports filtering, |
| 11 | +pattern matching, and can output results as objects. The function is |
| 12 | +designed to work with pipeline input and provides options for recursion, |
| 13 | +alternate data streams, and more. |
| 14 | +
|
| 15 | +.PARAMETER Input |
| 16 | +Input object containing file names or directories. Accepts pipeline input. |
| 17 | +
|
| 18 | +.PARAMETER File |
| 19 | +Return only files in the output. |
| 20 | +
|
| 21 | +.PARAMETER Pattern |
| 22 | +Regular expression pattern to search within content. |
| 23 | +
|
| 24 | +.PARAMETER RelativeBasePath |
| 25 | +Base path for resolving relative paths in output. |
| 26 | +
|
| 27 | +.PARAMETER AllDrives |
| 28 | +Search across all available drives. |
| 29 | +
|
| 30 | +.PARAMETER Directory |
| 31 | +Search for directories only. |
| 32 | +
|
| 33 | +.PARAMETER FilesAndDirectories |
| 34 | +Include both files and directories in the output. |
| 35 | +
|
| 36 | +.PARAMETER PassThru |
| 37 | +Output matched items as objects. |
| 38 | +
|
| 39 | +.PARAMETER IncludeAlternateFileStreams |
| 40 | +Include alternate data streams in search results. |
| 41 | +
|
| 42 | +.PARAMETER NoRecurse |
| 43 | +Do not recurse into subdirectories. |
| 44 | +
|
| 45 | +.EXAMPLE |
| 46 | +ResolveInputObjectFileNames -Input "C:\Temp" -File |
| 47 | +
|
| 48 | +#> |
| 49 | +############################################################################### |
| 50 | +function ResolveInputObjectFileNames { |
| 51 | + |
| 52 | + |
| 53 | + [CmdletBinding(DefaultParameterSetName = 'Default')] |
| 54 | + |
| 55 | + |
| 56 | + param( |
| 57 | + |
| 58 | + ########################################################################### |
| 59 | + [parameter( |
| 60 | + Position = 0, |
| 61 | + Mandatory = $false, |
| 62 | + HelpMessage = 'Input object containing file names or directories', |
| 63 | + ValueFromPipeline = $true, |
| 64 | + ValueFromPipelineByPropertyName = $true |
| 65 | + )] |
| 66 | + [Alias('Path', 'FilePath', 'Input')] |
| 67 | + [Object]$InputObject, |
| 68 | + |
| 69 | + ########################################################################### |
| 70 | + [parameter( |
| 71 | + Mandatory = $false, |
| 72 | + HelpMessage = 'Return only files' |
| 73 | + )] |
| 74 | + [switch]$File, |
| 75 | + |
| 76 | + ########################################################################### |
| 77 | + [Parameter( |
| 78 | + Position = 1, |
| 79 | + Mandatory = $false, |
| 80 | + ParameterSetName = 'WithPattern', |
| 81 | + HelpMessage = 'Regular expression pattern to search within content' |
| 82 | + )] |
| 83 | + [Alias('mc', 'matchcontent')] |
| 84 | + [ValidateNotNull()] |
| 85 | + [SupportsWildcards()] |
| 86 | + [string] $Pattern, |
| 87 | + |
| 88 | + ########################################################################### |
| 89 | + [Parameter( |
| 90 | + Position = 2, |
| 91 | + Mandatory = $false, |
| 92 | + HelpMessage = 'Base path for resolving relative paths in output' |
| 93 | + )] |
| 94 | + [Alias('base')] |
| 95 | + [ValidateNotNullOrEmpty()] |
| 96 | + [string] $RelativeBasePath, |
| 97 | + |
| 98 | + ########################################################################### |
| 99 | + [Parameter( |
| 100 | + Mandatory = $false, |
| 101 | + HelpMessage = 'Search across all available drives' |
| 102 | + )] |
| 103 | + |
| 104 | + [switch] $AllDrives, |
| 105 | + |
| 106 | + ########################################################################### |
| 107 | + [Parameter( |
| 108 | + Mandatory = $false, |
| 109 | + ParameterSetName = 'DirectoriesOnly', |
| 110 | + HelpMessage = 'Search for directories only' |
| 111 | + )] |
| 112 | + [Alias('dir')] |
| 113 | + [switch] $Directory, |
| 114 | + |
| 115 | + ########################################################################### |
| 116 | + [Parameter( |
| 117 | + Mandatory = $false, |
| 118 | + ParameterSetName = 'DirectoriesOnly', |
| 119 | + HelpMessage = 'Include both files and directories' |
| 120 | + )] |
| 121 | + [Alias('both')] |
| 122 | + [switch] $FilesAndDirectories, |
| 123 | + |
| 124 | + ########################################################################### |
| 125 | + [Parameter( |
| 126 | + Mandatory = $false, |
| 127 | + HelpMessage = 'Output matched items as objects' |
| 128 | + )] |
| 129 | + [Alias('pt')] |
| 130 | + [switch]$PassThru, |
| 131 | + |
| 132 | + ########################################################################### |
| 133 | + [Parameter( |
| 134 | + Mandatory = $false, |
| 135 | + HelpMessage = 'Include alternate data streams in search results' |
| 136 | + )] |
| 137 | + [Alias('ads')] |
| 138 | + [switch] $IncludeAlternateFileStreams, |
| 139 | + |
| 140 | + ########################################################################### |
| 141 | + [Parameter( |
| 142 | + Mandatory = $false, |
| 143 | + HelpMessage = 'Do not recurse into subdirectories' |
| 144 | + )] |
| 145 | + [switch] $NoRecurse |
| 146 | + ########################################################################### |
| 147 | + ) |
| 148 | + |
| 149 | + begin { |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + process { |
| 154 | + |
| 155 | + # return if input is null |
| 156 | + if ($null -eq $InputObject) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + # handle input as FileInfo object |
| 161 | + if ($InputObject -is [System.IO.FileInfo]) { |
| 162 | + |
| 163 | + # copy parameters for Find-Item call |
| 164 | + $findParams = GenXdev.Helpers\Copy-IdenticalParamValues ` |
| 165 | + -BoundParameters $PSBoundParameters ` |
| 166 | + -FunctionName 'GenXdev.FileSystem\Find-Item' ` |
| 167 | + -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue) |
| 168 | + |
| 169 | + # find the item using Find-Item |
| 170 | + $item = GenXdev.FileSystem\Find-Item -SearchMask ("$($InputObject.FullName)") ` |
| 171 | + @findParams -NoRecurse -File | |
| 172 | + Microsoft.PowerShell.Utility\Select-Object -First 1 |
| 173 | + |
| 174 | + # output item if found, otherwise verbose skip |
| 175 | + if ($item) { |
| 176 | + Microsoft.PowerShell.Utility\Write-Verbose ( |
| 177 | + "Item $($InputObject.FullName) included" |
| 178 | + ) |
| 179 | + Microsoft.PowerShell.Utility\Write-Output $item |
| 180 | + } |
| 181 | + else { |
| 182 | + Microsoft.PowerShell.Utility\Write-Verbose ( |
| 183 | + "Item $($InputObject.FullName) skipped due to filters" |
| 184 | + ) |
| 185 | + } |
| 186 | + |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + # handle input as DirectoryInfo object |
| 191 | + if ($InputObject -is [System.IO.DirectoryInfo]) { |
| 192 | + |
| 193 | + # if filter is present, process each filter value |
| 194 | + if ($Filter) { |
| 195 | + # if multiple filters, use Find-Item for each |
| 196 | + if ($Filter.Count -gt 1) { |
| 197 | + foreach ($f in $Filter) { |
| 198 | + # copy parameters for Find-Item call |
| 199 | + $params = GenXdev.Helpers\Copy-IdenticalParamValues ` |
| 200 | + -BoundParameters $PSBoundParameters ` |
| 201 | + -FunctionName 'GenXdev.FileSystem\Find-Item' ` |
| 202 | + -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue) |
| 203 | + |
| 204 | + # enumerate items for each filter |
| 205 | + GenXdev.FileSystem\Find-Item -SearchMask ("$($InputObject.FullName)\$f") ` |
| 206 | + @params | |
| 207 | + Microsoft.PowerShell.Core\ForEach-Object { |
| 208 | + |
| 209 | + # copy parameters for recursive expansion |
| 210 | + $expandParams = GenXdev.Helpers\Copy-IdenticalParamValues ` |
| 211 | + -BoundParameters $PSBoundParameters ` |
| 212 | + -FunctionName 'GenXdev.FileSystem\ResolveInputObjectFileNames' ` |
| 213 | + -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue); |
| 214 | + |
| 215 | + $expandParams.InputObject = "$($InputObject.FullName)\$_" |
| 216 | + GenXdev.FileSystem\ResolveInputObjectFileNames @expandParams | |
| 217 | + Microsoft.PowerShell.Core\ForEach-Object { |
| 218 | + Microsoft.PowerShell.Utility\Write-Output $_ |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + return; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + # copy parameters for Find-Item call |
| 227 | + $params = GenXdev.Helpers\Copy-IdenticalParamValues ` |
| 228 | + -BoundParameters $PSBoundParameters ` |
| 229 | + -FunctionName 'GenXdev.FileSystem\Find-Item' ` |
| 230 | + -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue) |
| 231 | + |
| 232 | + # enumerate items found by Find-Item |
| 233 | + GenXdev.FileSystem\Find-Item -SearchMask "$($InputObject.FullName)\*" -NoRecurse @params | |
| 234 | + Microsoft.PowerShell.Core\ForEach-Object { |
| 235 | + |
| 236 | + # skip directories if -File is specified |
| 237 | + if (($_ -is [System.IO.DirectoryInfo]) -and ($File)) { return } |
| 238 | + # skip string paths that are directories if -File is specified |
| 239 | + if (($_ -is [string]) -and [System.IO.Directory]::Exists((GenXdev.FileSystem\Expand-Path $_)) -and ($File)) { return } |
| 240 | + |
| 241 | + Microsoft.PowerShell.Utility\Write-Output $_ |
| 242 | + } |
| 243 | + |
| 244 | + return; |
| 245 | + } |
| 246 | + |
| 247 | + # handle input as enumerable collection |
| 248 | + if (($InputObject -isnot [string]) -and ($InputObject -is [System.Collections.IEnumerable])) { |
| 249 | + |
| 250 | + # expand each item in the collection |
| 251 | + $InputObject | Microsoft.PowerShell.Core\ForEach-Object { |
| 252 | + |
| 253 | + $a = $_ |
| 254 | + # Handle objects with Key/Value properties (e.g., DictionaryEntry, hashtable, PSCustomObject) |
| 255 | + if ($a -is [System.Collections.DictionaryEntry]) { |
| 256 | + $a = $a.Value |
| 257 | + } elseif ($a -is [hashtable]) { |
| 258 | + $a = $a.Values |
| 259 | + } elseif ($a.PSObject.Properties.Match('Key').Count -gt 0 -and $a.PSObject.Properties.Match('Value').Count -gt 0) { |
| 260 | + $a = $a.Value |
| 261 | + } |
| 262 | + $a | Microsoft.PowerShell.Core\ForEach-Object { |
| 263 | + |
| 264 | + $expandParams = GenXdev.Helpers\Copy-IdenticalParamValues ` |
| 265 | + -BoundParameters $PSBoundParameters ` |
| 266 | + -FunctionName 'GenXdev.FileSystem\ResolveInputObjectFileNames' ` |
| 267 | + -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue); |
| 268 | + |
| 269 | + @(GenXdev.FileSystem\ResolveInputObjectFileNames @expandParams -InputObject $PSItem) | |
| 270 | + Microsoft.PowerShell.Core\ForEach-Object { |
| 271 | + Microsoft.PowerShell.Utility\Write-Output $_ |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + return; |
| 276 | + } |
| 277 | + |
| 278 | + # return if input is not a string or is empty/whitespace |
| 279 | + if ((-not ($InputObject -is [string])) -or [string]::IsNullOrWhiteSpace($InputObject)) { |
| 280 | + return; |
| 281 | + } |
| 282 | + |
| 283 | + # copy parameters for Find-Item call |
| 284 | + $params = GenXdev.Helpers\Copy-IdenticalParamValues ` |
| 285 | + -BoundParameters $PSBoundParameters ` |
| 286 | + -FunctionName 'GenXdev.FileSystem\Find-Item' ` |
| 287 | + -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue) |
| 288 | + |
| 289 | + # enumerate items found by Find-Item |
| 290 | + GenXdev.FileSystem\Find-Item -SearchMask $InputObject @params | |
| 291 | + Microsoft.PowerShell.Core\ForEach-Object { |
| 292 | + |
| 293 | + # skip directories if -File is specified |
| 294 | + if (($_ -is [System.IO.DirectoryInfo]) -and ($File)) { return } |
| 295 | + # skip string paths that are directories if -File is specified |
| 296 | + if (($_ -is [string]) -and [System.IO.Directory]::Exists((GenXdev.FileSystem\Expand-Path $_)) -and ($File)) { return } |
| 297 | + |
| 298 | + Microsoft.PowerShell.Utility\Write-Output $_ |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + end { |
| 303 | + |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +############################################################################### |
0 commit comments