Last active
January 24, 2018 15:16
-
-
Save marckassay/76a7560e7c7f9b320fb3aa12e663631b to your computer and use it in GitHub Desktop.
Similarly to Get-ChildItem, this function can recurse the -Path parameter parent or parents for a file or folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Similarly to Get-ChildItem, this function can recurse the -Path parameter parent or parents | |
for a file or folder | |
.DESCRIPTION | |
Long description | |
.PARAMETER Path | |
The path to start | |
.PARAMETER Name | |
The name of the item to seek for. Must include extention of file. | |
.PARAMETER Recurse | |
If not switched, it will only look at the directory above -Path. | |
.PARAMETER File | |
Seems to be ineffective | |
.PARAMETER Directory | |
Seems to be ineffective | |
.PARAMETER Force | |
Seems to be ineffective | |
.EXAMPLE | |
Using this function piped with Test-Path | |
E:\> Get-ParentItem E:\Temp\AIT\src\pages\ -Name '.gitignore' -Verbose -Recurse | Test-Path -PathType Leaf | |
VERBOSE: Starting with: E:\Temp\AIT\src\pages\ | |
VERBOSE: CurrentDirectory var is: E:\Temp\AIT\src\pages\ | |
VERBOSE: Found item, if there is one: | |
VERBOSE: Recursing function with: E:\Temp\AIT\src | |
VERBOSE: Starting with: E:\Temp\AIT\src | |
VERBOSE: CurrentDirectory var is: E:\Temp\AIT\src | |
VERBOSE: Found item, if there is one: | |
VERBOSE: Recursing function with: E:\Temp\AIT | |
VERBOSE: Starting with: E:\Temp\AIT | |
VERBOSE: CurrentDirectory var is: E:\Temp\AIT | |
VERBOSE: Found item, if there is one: .gitignore | |
True | |
E:\> | |
.LINK | |
https://gist.github.com/marckassay/76a7560e7c7f9b320fb3aa12e663631b | |
.NOTES | |
General notes | |
#> | |
function Get-ParentItem { | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory = $true, Position = 1)] | |
[ValidateNotNullOrEmpty()] | |
[string[]]$Path, | |
[Parameter(Mandatory = $true, Position = 2)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Name, | |
[switch]$Recurse, | |
[switch]$File, | |
[switch]$Directory, | |
[switch]$Force | |
) | |
Write-Verbose ("Starting with: $Path") | |
if ($Path | Test-Path -PathType Leaf) { | |
Write-Verbose (" this is a leaf, getting its directory name...") | |
$Path = $Path | Get-ItemPropertyValue -Name DirectoryName | |
Write-Verbose (" now using: $Path") | |
} | |
$CurrentDirectory = $(Get-Item $Path) | |
Write-Verbose (" CurrentDirectory var is: $CurrentDirectory") | |
$FoundItem = Get-ChildItem -Path $CurrentDirectory ` | |
-Filter $Name ` | |
-File:$File.IsPresent ` | |
-Directory:$Directory.IsPresent ` | |
-Force:$Force.IsPresent | |
Write-Verbose (" Found item, if there is one: $FoundItem") | |
if (($Recurse.IsPresent -eq $true) -and ($FoundItem -eq $null) -and ($CurrentDirectory.Parent.FullName -ne $null)) { | |
Write-Verbose (" Recursing function with: " + $CurrentDirectory.Parent.FullName) | |
Get-ParentItem -Path $CurrentDirectory.Parent.FullName -Name $Name -Recurse | |
} | |
else { | |
$FoundItem | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment