PowerShell:如何递归地测试目录树上的路径?

PowerShell:如何递归地测试目录树上的路径?,powershell,Powershell,我似乎不知道如何爬上目录并检查每个父文件夹中的项目。也就是说,从ever get location所在的位置,我想检查(get location).parent,然后检查(get location).parent.parent等等,直到我回到根目录,c: 这是我到目前为止所了解到的,但它看起来既麻烦又笨拙。我得到它,看看它是否存在,我应该使用类似的东西: test-path (get-item $t).Parent.Parent $p = split-path $(get-location)

我似乎不知道如何爬上目录并检查每个父文件夹中的项目。也就是说,从ever get location所在的位置,我想检查(get location).parent,然后检查(get location).parent.parent等等,直到我回到根目录,c:

这是我到目前为止所了解到的,但它看起来既麻烦又笨拙。我得到它,看看它是否存在,我应该使用类似的东西:

test-path (get-item $t).Parent.Parent
$p = split-path $(get-location) -Parent
if ( (split-path $p -leaf) -eq "test" ) {
    $q = $(Split-Path $p -parent ) 
} elseif ( (split-path $q -leaf) -eq "test") {
    $r = (split-path $q -leaf) -eq "test"
}
但这不会像我想的那样爬上目录树,我也无法探索如何将它与分割路径结合使用

我的想法是:

test-path (get-item $t).Parent.Parent
$p = split-path $(get-location) -Parent
if ( (split-path $p -leaf) -eq "test" ) {
    $q = $(Split-Path $p -parent ) 
} elseif ( (split-path $q -leaf) -eq "test") {
    $r = (split-path $q -leaf) -eq "test"
}

可能有用,但它不像我希望的那样递归。

我必须诚实。为什么要测试父级的路径?如果子对象存在,则父对象也存在。
# The name of the file to look for.
$fileName = 'somefile.txt'

# Where to start looking (the current dir).
$dir = $PWD.ProviderPath

# Walk up the directory hierarchy until the first file whose name
# matches $fileName is found, and return that file's path.
# If no such file is found, $file is (effectively) $null.
$file = 
 do { 
   if (Test-Path -Type Leaf -LiteralPath ($file = "$dir/$fileName")) { 
     $file # output the path of the file found.
     break # exit the loop
   }
 } while ($dir = Split-Path -LiteralPath $dir)